Repository navigation
fix: harden catch blocks against non-Error throws (#142, #147) - #154
Conversation
Replace every remaining (error as Error).message inside catch blocks with error instanceof Error ? error.message : String(error). When a non-Error value (null, string, number) is thrown, the cast form crashes the catch itself — (null).message throws TypeError — defeating the catch's swallow-and-log contract. #141 already fixed hookManager.ts (callOnLogin/Logout/TokenRefresh/ callResolveProvider/callOnMCPTokenIssued) and token.ts / withMCPAuth.ts. This sweep fixes all remaining sites found by exhaustive grep: src/index.ts:313 — session validator provider resolution src/lib/sessionValidator.ts:97 — token validation error log src/lib/sessionValidator.ts:181 — token refresh error log src/lib/sessionValidator.ts:186 — Token refresh failed: error message src/lib/resource.ts:381 — dynamic provider resolution src/lib/OAuthProvider.ts:161 — additional user info fetch fallback src/lib/OAuthProvider.ts:225-226 — ID token signature verification log + rethrow src/lib/handlers.ts:239 — ID token verification fallback log src/lib/handlers.ts:332 — OAuth callback error reason derivation src/lib/providers/github.ts:42 — GitHub token validation error src/lib/providers/github.ts:76 — GitHub email fetch failure src/lib/mcp/dcr.ts:296 — client registration storage failure src/lib/mcp/wellKnown.ts:273 — well-known handler failure src/lib/mcp/authorize.ts:203 — client lookup failure src/lib/mcp/authorize.ts:292 — CSRF token generation failure src/lib/mcp/callback.ts:93 — auth code persistence failure Tests: add 4 non-Error throw assertions in hookManager.test.js for callOnLogin (string + null), callOnLogout (null), and callOnTokenRefresh (string) — the async-await methods that are easily exercised without DB mocking. The mcp store catches (dcr/authorize/callback/wellKnown) are mechanical compiler-verified changes over DB-wrapping code. Closes #142 Closes #147 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Code Review
This pull request improves error handling safety across the codebase by replacing unsafe type assertions on caught errors with explicit instanceof Error checks, and adds unit tests to verify behavior when non-Error values are thrown. The reviewer feedback is highly constructive, recommending that raw error objects be passed directly to loggers to preserve stack traces and metadata, and suggesting the use of the cause option when wrapping and re-throwing errors to maintain the original error context.
|
Reviewed; no blockers found. |
|
Reviewed; no blockers found. The PR correctly hardens error handling across the plugin by replacing unsafe The new unit tests for |
Gemini review on #154: preserve the original error via Error cause when wrapping the ID-token verification failure, per HEG error-handling (cause chaining). Log sites keep the safe message ternary for consistency with the #141 convention. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Summary
(error as Error).messagein acatch (error)block crashes the catch itself when a non-Error value is thrown —(null).messagethrowsTypeError, defeating the "swallow and log" contract. This is a comprehensive sweep of all remaining occurrences after #141 fixedhookManager.ts,token.ts, andwithMCPAuth.ts.All 16 remaining sites replaced with
error instanceof Error ? error.message : String(error).Every catch site changed
src/index.tssrc/lib/sessionValidator.tssrc/lib/sessionValidator.tssrc/lib/sessionValidator.tsToken refresh failed:error stringsrc/lib/resource.tssrc/lib/OAuthProvider.tssrc/lib/OAuthProvider.tssrc/lib/handlers.tssrc/lib/handlers.tssrc/lib/providers/github.tssrc/lib/providers/github.tssrc/lib/mcp/dcr.tssrc/lib/mcp/wellKnown.tssrc/lib/mcp/authorize.tssrc/lib/mcp/authorize.tssrc/lib/mcp/callback.tsTests
Added 4 non-Error throw cases to
test/lib/hookManager.test.jsforcallOnLogin(string throw + null throw),callOnLogout(null throw), andcallOnTokenRefresh(string throw) — these are the async-await hook methods directly exercisable without a DB. The mcp store catch sites (dcr/authorize/callback/wellKnown) wrap DB calls that are not trivially mockable; those changes are mechanical and compiler-verified.Closes #142
Closes #147
🤖 Generated with Claude Code — Claude Sonnet 4.6