Merge pull request #648 from mendableai/nsc/fix-json-error-handler

Fixed 500 errors when JSON is badly formatted
This commit is contained in:
Nicolas 2024-09-09 12:28:18 -03:00 committed by GitHub
commit 31e973d915
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -201,16 +201,20 @@ if (cluster.isMaster) {
Sentry.setupExpressErrorHandler(app);
app.use((err: unknown, req: Request<{}, ErrorResponse, undefined>, res: ResponseWithSentry<ErrorResponse>, next: NextFunction) => {
if (err instanceof SyntaxError && 'status' in err && err.status === 400 && 'body' in err) {
return res.status(400).json({ success: false, error: 'Bad request, malformed JSON' });
}
const id = res.sentry ?? uuidv4();
let verbose = JSON.stringify(err);
if (verbose === "{}") {
if (err instanceof Error) {
verbose = JSON.stringify({
message: err.message,
name: err.name,
stack: err.stack,
});
}
if (err instanceof Error) {
verbose = JSON.stringify({
message: err.message,
name: err.name,
stack: err.stack,
});
}
}
Logger.error("Error occurred in request! (" + req.path + ") -- ID " + id + " -- " + verbose);