Skip to content

feat: implement .from() method for exception chaining - #4

Merged
codewizdave merged 1 commit into
devfrom
task/05-from-method
May 29, 2026
Merged

feat: implement .from() method for exception chaining#4
codewizdave merged 1 commit into
devfrom
task/05-from-method

Conversation

@martyy-code

Copy link
Copy Markdown
Contributor

Summary

Add .from(cause) method to ErrorInstance for exception chaining:

consterr=ValidationError({field: 'email'}).from(newNetworkError('Connection failed'));

Features

  • Cause chaining: Sets err.cause property
  • Full chain: Maintains err.causes array with complete history
  • Method chaining: err.from(a).from(b) works
  • Native support: Works with native JS errors
  • Nested chains: Preserves nested cause chains

Implementation

  • Added .from() method to ErrorInstance type
  • Method returns instance for chaining
  • Causes array maintains chronological order: newest first

Test plan

  • 67 tests passing
  • TypeScript strict mode passes
  • ESLint passes

🤖 Generated with Claude Code

Add .from(cause) method to ErrorInstance:
- Sets the cause property on the error
- Maintains full cause chain in causes array
- Supports method chaining (err.from(a).from(b))
- Works with native errors and custom error factories
- Preserves nested cause chains
Implementation:
- Added .from() method to ErrorInstance type
- Method returns instance for chaining
- Causes array maintains chronological order: newest first
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

@martyy-codemartyy-code left a comment

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Review: feat: implement .from() method for exception chaining

Summary

The .from() implementation for exception chaining is well-designed and correctly handles the core requirements. The cause chain building logic properly preserves nested cause arrays and maintains chronological order.

What Works Well

  • Correct cause chain building: The spread order [cause, ...cause.causes, ...instance.causes] correctly maintains newest-first ordering
  • Handles native errors: Properly defaults to [] when cause has no causes property (native Error objects)
  • Type signature is correct: from(cause: Error | ErrorInstance): ErrorInstance<TFields> matches the implementation that checks for causes property
  • Method chaining: Returns instance allowing .from(a).from(b) pattern
  • Test coverage: Comprehensive tests covering basic usage, nested chains, chaining, type safety, and edge cases

Suggestion (Non-blocking)

The type guard 'causes' in cause && Array.isArray(cause.causes) could theoretically fail if someone attached a non-array causes property to an error. However, this is an extremely unlikely scenario that does not warrant a blocking issue.

Recommendation

Approve - The implementation is clean, well-tested, and follows the design correctly.

Sign up for freeto 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.

2 participants

@martyy-code@codewizdave