From dd55d6961280e2fdd9dfbbc4405bf59b18f12639 Mon Sep 17 00:00:00 2001 From: Bobby Wibowo Date: Sat, 9 Jan 2021 03:50:03 +0700 Subject: [PATCH] fix: headers already sent errors --- controllers/handlers/apiErrorsHandler.js | 6 ++---- lolisafe.js | 12 ++++++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/controllers/handlers/apiErrorsHandler.js b/controllers/handlers/apiErrorsHandler.js index 66b30ea..3e69956 100644 --- a/controllers/handlers/apiErrorsHandler.js +++ b/controllers/handlers/apiErrorsHandler.js @@ -21,15 +21,13 @@ module.exports = (error, req, res, next) => { ? error.statusCode : 500 - res.status(statusCode) - const description = (isClientError || isServerError) ? error.message : 'An unexpected error occurred. Try again?' if (description) { - return res.json({ success: false, description }) + return res.status(statusCode).json({ success: false, description }) } else { - return res.end() + return res.status(statusCode).end() } } diff --git a/lolisafe.js b/lolisafe.js index ffc3a93..099270a 100644 --- a/lolisafe.js +++ b/lolisafe.js @@ -254,14 +254,18 @@ safe.use('/api', api) // Error pages safe.use((req, res, next) => { - res.setHeader('Cache-Control', 'no-store') - res.status(404).sendFile(path.join(paths.errorRoot, config.errorPages[404])) + if (!res.headersSent) { + res.setHeader('Cache-Control', 'no-store') + res.status(404).sendFile(path.join(paths.errorRoot, config.errorPages[404])) + } }) safe.use((error, req, res, next) => { logger.error(error) - res.setHeader('Cache-Control', 'no-store') - res.status(500).sendFile(path.join(paths.errorRoot, config.errorPages[500])) + if (!res.headersSent) { + res.setHeader('Cache-Control', 'no-store') + res.status(500).sendFile(path.join(paths.errorRoot, config.errorPages[500])) + } }) // Git hash