Skip to content

Commit

Permalink
Explicitly handle JSON parsing errors
Browse files Browse the repository at this point in the history
If invalid JSON is sent, no longer gives an internal server error.
Instead, we give bad request.
  • Loading branch information
Timothy-Gonzalez committed Jan 13, 2025
1 parent ff0d231 commit 62f63d9
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/middleware/error-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ export function ErrorHandler(error: unknown, req: Request, res: Response, _next:
context: error.context,
});
}
// Handle JSON parsing syntax error
if (error instanceof SyntaxError) {
res.status(StatusCode.ClientErrorBadRequest).send({
error: "BadRequest",
message: "Bad request made - unable to parse the request. Are you sending valid JSON?",
});
}
// Otherwise, undefined error - so we display default internal error
const userId = tryGetAuthenticatedUser(req)?.id || "unauthenticated";
const id = randomUUID();
Expand Down

0 comments on commit 62f63d9

Please sign in to comment.