Skip to content

Add exitCode property as alias for code in error objects#110

Open
konard wants to merge 3 commits into
mainfrom
issue-38-8ff5f784
Open

Add exitCode property as alias for code in error objects#110
konard wants to merge 3 commits into
mainfrom
issue-38-8ff5f784

Conversation

@konard

@konard konard commented Sep 9, 2025

Copy link
Copy Markdown
Member

🤖 AI-Powered Solution

This pull request automatically solves issue #38 by adding error.exitCode as an alias for error.code to maintain compatibility with Node.js standard error handling patterns.

📋 Issue Reference

Fixes #38

🔧 What Changed

Core Implementation

  • Added error.exitCode property: All error objects now include both error.code and error.exitCode with the same value
  • Fixed virtual exit command: Now throws proper Error objects instead of plain objects
  • Maintained backward compatibility: Existing code using error.code continues to work unchanged

Modified Files

  • src/$.mjs: Added error.exitCode = <code> alongside every error.code = <code> assignment
  • src/commands/$.exit.mjs: Fixed to throw proper Error objects with both properties
  • tests/exitcode-compatibility.test.mjs: Comprehensive test suite for both patterns
  • examples/test-exitcode-compatibility.mjs: Demonstration script showing compatibility

✅ Solution Details

The fix implements Option 1: Add Alias (Backward Compatible) from the issue description:

// Before (command-stream only)
catch (error) {
  if (error.code === 1) { /* handle */ }
  // error.exitCode was undefined ❌
}

// After (both patterns work)
catch (error) {
  if (error.code === 1) { /* handle */ }     // ✅ Still works
  if (error.exitCode === 1) { /* handle */ } // ✅ Now works too
}

🧪 Testing

All error scenarios now provide both properties:

  • Command execution failures (errexit)
  • Pipeline failures (pipefail)
  • Virtual command failures (exit, etc.)
  • File system errors

Test Coverage:

  • ✅ Both properties exist and have same value
  • ✅ Backward compatibility with existing error.code usage
  • ✅ Forward compatibility with Node.js standard error.exitCode
  • ✅ Pipeline error handling
  • ✅ Multiple exit code values (1, 2, 127, 255)
  • ✅ File system error handling

📊 Compatibility Matrix

Library Exit Code Property Status
Node.js child_process error.exitCode ✅ Compatible
execa error.exitCode ✅ Compatible
cross-spawn error.exitCode ✅ Compatible
command-stream (before) error.code ❌ Non-standard
command-stream (after) error.code + error.exitCode ✅ Both supported

🚀 Impact

This change enables:

  • Easy migration from other command execution libraries
  • Standard Node.js patterns without modification
  • Zero breaking changes to existing code
  • Improved developer experience following principle of least surprise

This PR was automatically generated and tested by the AI issue solver

Adding CLAUDE.md with task information for AI processing.
This file will be removed when the task is complete.

Issue: #38
@konard konard self-assigned this Sep 9, 2025
konard and others added 2 commits September 9, 2025 20:58
This change addresses issue #38 by adding error.exitCode as an alias for
error.code to maintain compatibility with Node.js standard error handling
patterns while preserving backward compatibility.

Changes:
- Add error.exitCode property alongside error.code in all error creation locations
- Fix $.exit.mjs virtual command to throw proper Error objects instead of plain objects
- Add comprehensive tests for exitCode compatibility
- Add example script demonstrating both old and new error handling patterns

Both error.code and error.exitCode now contain the same exit code value,
allowing developers to use either the traditional command-stream pattern
or the standard Node.js pattern.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@konard konard changed the title [WIP] The library uses error.code instead of error.exitCode Add exitCode property as alias for code in error objects Sep 9, 2025
@konard konard marked this pull request as ready for review September 9, 2025 18:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The library uses error.code instead of error.exitCode

1 participant