Skip to content

Commit f322e19

Browse files
committed
fix: add json parse error handler
1 parent b19cb3a commit f322e19

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

src/presentation/http/http-api.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import UploadRouter from './router/upload.js';
3434
import { ajvFilePlugin } from '@fastify/multipart';
3535
import { UploadSchema } from './schema/Upload.js';
3636
import { NoteHierarchySchema } from './schema/NoteHierarchy.js';
37+
import { StatusCodes } from 'http-status-codes';
3738

3839
const appServerLogger = getLogger('appServer');
3940

@@ -372,6 +373,21 @@ export default class HttpApi implements Api {
372373

373374
return;
374375
}
376+
/**
377+
* JSON parse errors (invalid request body)
378+
*/
379+
if (error instanceof SyntaxError && error.message.includes('JSON')) {
380+
this.log.warn({ reqId: request.id }, 'Invalid JSON in request body');
381+
382+
return reply
383+
.code(StatusCodes.BAD_REQUEST)
384+
.type('application/json')
385+
.send({
386+
message: 'Invalid JSON in request body',
387+
error: 'Bad Request',
388+
statusCode: StatusCodes.BAD_REQUEST,
389+
});
390+
}
375391
/**
376392
* If error is not a domain error, we route it to the default error handler
377393
*/

0 commit comments

Comments
 (0)