Open
Conversation
Coverage Report
File Coverage
|
||||||||||||||||||||||||||||||||||||||
neSpecc
approved these changes
Apr 1, 2026
There was a problem hiding this comment.
Pull request overview
Adds explicit handling for invalid JSON request bodies in the Fastify global error handler, aiming to return a consistent 400 response instead of delegating to generic error handling.
Changes:
- Import
StatusCodesfor standardized HTTP status constants. - Add a
SyntaxErrorhandling branch insetErrorHandlerthat logs and returns a 400 JSON response for invalid JSON bodies.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+376
to
+389
| /** | ||
| * JSON parse errors (invalid request body) | ||
| */ | ||
| if (error instanceof SyntaxError && error.message.includes('JSON')) { | ||
| this.log.warn({ reqId: request.id }, 'Invalid JSON in request body'); | ||
|
|
||
| return reply | ||
| .code(StatusCodes.BAD_REQUEST) | ||
| .type('application/json') | ||
| .send({ | ||
| message: 'Invalid JSON in request body', | ||
| error: 'Bad Request', | ||
| statusCode: StatusCodes.BAD_REQUEST, | ||
| }); |
There was a problem hiding this comment.
New behavior in the global error handler should be covered by tests. There is existing Vitest coverage for HTTP routes using global.api.fakeRequest (e.g., src/presentation/http/router/*.test.ts); please add a test that sends invalid JSON with content-type: application/json and asserts the API returns 400 with the expected error payload.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds explicit handling for JSON parsing errors in the global error handler. When a request contains invalid JSON, the server now returns a consistent 400 Bad Request response instead of falling through to generic error handling.