fix: headers already sent errors

This commit is contained in:
Bobby Wibowo 2021-01-09 03:50:03 +07:00
parent 9dc52774be
commit dd55d69612
No known key found for this signature in database
GPG Key ID: 51C3A1E1E22D26CF
2 changed files with 10 additions and 8 deletions

View File

@ -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()
}
}

View File

@ -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