-
Notifications
You must be signed in to change notification settings - Fork 2
fix: add json parse error handler #300
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -34,6 +34,7 @@ import UploadRouter from './router/upload.js'; | |||||
| import { ajvFilePlugin } from '@fastify/multipart'; | ||||||
| import { UploadSchema } from './schema/Upload.js'; | ||||||
| import { NoteHierarchySchema } from './schema/NoteHierarchy.js'; | ||||||
| import { StatusCodes } from 'http-status-codes'; | ||||||
|
|
||||||
| const appServerLogger = getLogger('appServer'); | ||||||
|
|
||||||
|
|
@@ -372,6 +373,21 @@ export default class HttpApi implements Api { | |||||
|
|
||||||
| return; | ||||||
| } | ||||||
| /** | ||||||
| * 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, | ||||||
|
Comment on lines
+387
to
+388
|
||||||
| error: 'Bad Request', | |
| statusCode: StatusCodes.BAD_REQUEST, |
Copilot
AI
Apr 1, 2026
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The JSON-parse branch matches any
SyntaxErrorwhose message contains "JSON", which can inadvertently convert server-side JSON.parse bugs into 400 responses and make them harder to detect. Consider narrowing the condition to Fastify body-parser errors (e.g., checkerror.statusCode === 400and/or a Fastify-specificerror.code/bodymarker) so only invalid request bodies are handled here.