Browse Source

fix: headers already sent errors

master^2
Bobby Wibowo 5 months ago
parent
commit
dd55d69612
No known key found for this signature in database GPG Key ID: 51C3A1E1E22D26CF
  1. 6
      controllers/handlers/apiErrorsHandler.js
  2. 12
      lolisafe.js

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

12
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

Loading…
Cancel
Save