Skip to content

fix: add json parse error handler#300

Open
7eliassen wants to merge 1 commit intomainfrom
fix/298-server-crashes-on-post
Open

fix: add json parse error handler#300
7eliassen wants to merge 1 commit intomainfrom
fix/298-server-crashes-on-post

Conversation

@7eliassen
Copy link
Copy Markdown

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.

@github-actions
Copy link
Copy Markdown

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 85.82% (🎯 80%)
⬇️ -0.09%
8938 / 10414
🔵 Statements 85.82% (🎯 80%)
⬇️ -0.09%
8938 / 10414
🔵 Functions 79.53% (🎯 80%)
🟰 ±0%
276 / 347
🔵 Branches 84.28% (🎯 80%)
⬇️ -0.29%
445 / 528
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
src/presentation/http/http-api.ts 93.45%
⬇️ -2.61%
85.29%
⬇️ -5.03%
93.75%
🟰 ±0%
93.45%
⬇️ -2.61%
103-111, 122-123, 325-326, 349-350, 380-390
Generated in workflow #912 for commit f322e19 by the Vitest Coverage Report Action

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 StatusCodes for standardized HTTP status constants.
  • Add a SyntaxError handling branch in setErrorHandler that 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,
});
Copy link

Copilot AI Apr 1, 2026

Choose a reason for hiding this comment

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

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.

Copilot uses AI. Check for mistakes.
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.

Server crashes on POST requests with incorrect Content-Length or incorrect JSON body

3 participants