Skip to content

fix: rethrow normalized Error in tryCatchIf/asyncTryCatchIf#2930

Merged
la14-1 merged 1 commit intoOpenRouterTeam:mainfrom
AhmedTMM:fix/trycatchif-rethrow-normalized
Mar 24, 2026
Merged

fix: rethrow normalized Error in tryCatchIf/asyncTryCatchIf#2930
la14-1 merged 1 commit intoOpenRouterTeam:mainfrom
AhmedTMM:fix/trycatchif-rethrow-normalized

Conversation

@AhmedTMM
Copy link
Collaborator

Summary

  • tryCatchIf and asyncTryCatchIf normalize caught values to Error instances but then re-threw the raw original value when the guard returned false
  • If something threw a string or number, downstream code expecting Error instances (.message, .stack) would break
  • Two-character fix: throw ethrow err in both functions
  • Also reformatted test file to pass biome (existing formatting was non-compliant)

Test plan

  • Biome lint passes (0 errors)
  • bun test src/__tests__/result.test.ts passes (32/32, 2 new)

🤖 Generated with Claude Code

Copy link
Member

@louisgv louisgv left a comment

Choose a reason for hiding this comment

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

Security Review

Verdict: APPROVED
Commit: 2fadecb

Findings

None - this is a security improvement.

Analysis

The fix changes throw e; to throw err; in tryCatchIf/asyncTryCatchIf, ensuring re-thrown values are normalized Error instances rather than raw thrown values (strings, numbers, objects). This enhances type safety and prevents unsafe error handling.

Security improvements:

  • Type safety: ensures thrown values are always Error instances
  • Error normalization: prevents information leakage from malformed error objects
  • Stack trace preservation: normalized errors include proper stack traces
  • Aligns implementation with documented intent

Tests

  • bun test: PASS (32 tests, 0 failures)
  • biome lint: PASS (0 errors in changed files)
  • curl|bash: N/A (no shell scripts)
  • macOS compat: N/A (no shell scripts)

Test Coverage

The PR adds comprehensive test coverage for the fix:

  • Line 138-151: sync test for non-Error value re-throw normalization
  • Line 186-198: async test for non-Error value re-throw normalization

-- security/pr-reviewer

When the guard returns false, both functions re-threw the raw caught
value (e) instead of the normalized Error (err). If a non-Error value
was thrown (string, number), downstream handlers received inconsistent
types instead of always getting Error instances.

Changed throw e → throw err in both functions.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@la14-1 la14-1 force-pushed the fix/trycatchif-rethrow-normalized branch from 2fadecb to 7ec24d6 Compare March 24, 2026 02:31
@la14-1 la14-1 merged commit 8d73d73 into OpenRouterTeam:main Mar 24, 2026
5 checks passed
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.

3 participants