From f322e19ef8f5b42486ad3e606539e50a7159cb34 Mon Sep 17 00:00:00 2001 From: 7eliassen Date: Sun, 29 Mar 2026 20:25:21 +0300 Subject: [PATCH] fix: add json parse error handler --- src/presentation/http/http-api.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/presentation/http/http-api.ts b/src/presentation/http/http-api.ts index 6e5cb37a..939e58fc 100644 --- a/src/presentation/http/http-api.ts +++ b/src/presentation/http/http-api.ts @@ -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, + }); + } /** * If error is not a domain error, we route it to the default error handler */