From ca8a00d0e7cb71b77f8abc3a830810be9d50c9ad Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 1 Sep 2023 09:27:57 +0200 Subject: Fix ERR_HTTP_HEADERS_SENT crash --- server/server/lib/object-storage/proxy.ts | 2 ++ server/server/middlewares/error.ts | 11 ++++++----- 2 files changed, 8 insertions(+), 5 deletions(-) (limited to 'server') diff --git a/server/server/lib/object-storage/proxy.ts b/server/server/lib/object-storage/proxy.ts index bef899fed..af37029f8 100644 --- a/server/server/lib/object-storage/proxy.ts +++ b/server/server/lib/object-storage/proxy.ts @@ -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, diff --git a/server/server/middlewares/error.ts b/server/server/middlewares/error.ts index 706009795..51012af07 100644 --- a/server/server/middlewares/error.ts +++ b/server/server/middlewares/error.ts @@ -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) } -- cgit v1.2.3