gateway
gateway copied to clipboard
fix: correctly handle other errors from handlers
trafficstars
Author Description
Title:
This fixes an issue where some unhandled errors thrown by tryPost cause big issues. Due to some loose typing, it was possible for the handlers to return null for a response. This causes bad things as eventually it makes its way to the top of the call chain, and if you return from your last handler in hono, the context itself is returned, which is difficult to understand what is going on.
Title: fix: correctly handle other errors from handlers
๐ What Changed
- Added explicit typing for
responsevariable asResponse | null - Changed
error: anytoerror: unknownfor better type safety - Added proper type checking for error objects before accessing properties
- Added a safeguard that throws an error when no response is created
๐ Impact of the Change
- Prevents unhandled errors from causing unexpected behavior
- Ensures that a proper response is always returned or an error is thrown
- Improves type safety throughout the error handling flow
- Provides clearer error messages when something goes wrong
๐ Total Files Changed
- 1 file modified:
src/handlers/handlerUtils.ts(12 additions, 3 deletions)
๐งช Test Added
N/A
๐ Security Vulnerabilities
N/A
Quality Recommendations
-
Consider adding unit tests to verify the error handling behavior
-
Add more specific error messages that include details about what went wrong
-
Consider implementing a more robust error handling strategy with custom error types