fix(api): gracefully handle express body-parser 4xx client errors#100
Conversation
When `express.json()` processes requests with unsupported character sets, unsupported encodings, or aborted connections, it throws specific error types (`charset.unsupported`, `encoding.unsupported`, `request.aborted`). If unhandled, these fall through to the global generic error handler, which throws a 500 Internal Server Error and logs to the console, leading to potential log spam (DoS vector) and incorrect HTTP response codes. This patch: - Explicitly catches `charset.unsupported` and `encoding.unsupported` to return a 415 Unsupported Media Type. - Explicitly catches `request.aborted` to return a 400 Bad Request. - Adds comprehensive unit tests for these scenarios. - Avoids repetitive `Buffer` allocations by precomputing standard JSON responses for `415` and `400` errors. Co-authored-by: shenald-dev <245350826+shenald-dev@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
|
Looks good, merging this in. CI is green and everything checks out 👍 Adds graceful handling of express body-parser 4xx errors and corresponding tests. |
Acknowledged. |
Modified
src/index.jsglobal error handler to handle specificbody-parsererrors such ascharset.unsupported,encoding.unsupported, andrequest.abortedcorrectly instead of throwing 500 Server Errors. Also added tests totests/api_robustness.test.jsto assert the proper 415 and 400 statuses are returned when tested directly using a mockedexpressapp. Updated.jules/bolt.mdwith learnings from the fix.PR created automatically by Jules for task 7638144141725921686 started by @shenald-dev