Fix ERR_HTTP_HEADERS_SENT crash

pull/5975/head
Chocobozzz 2023-09-01 09:27:57 +02:00
parent 69601b66fe
commit ca8a00d0e7
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
2 changed files with 8 additions and 5 deletions

View File

@ -83,6 +83,8 @@ function handleObjectStorageFailure (res: express.Response, err: Error) {
return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
}
logger.error('Object storage failure', { err })
return res.fail({
status: HttpStatusCode.INTERNAL_SERVER_ERROR_500,
message: err.message,

View File

@ -17,11 +17,6 @@ function apiFailMiddleware (req: express.Request, res: express.Response, next: e
error: message
})
res.status(status)
if (!res.headersSent) {
res.setHeader('Content-Type', 'application/problem+json')
}
const json = new ProblemDocument({
status,
@ -37,6 +32,12 @@ function apiFailMiddleware (req: express.Request, res: express.Response, next: e
logger.debug('Bad HTTP request.', { json, tags })
res.status(status)
// Cannot display a proper error to the client since headers are already sent
if (res.headersSent) return
res.setHeader('Content-Type', 'application/problem+json')
res.json(json)
}