Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Determine status code in determined order
let statusCode = res.statusCode || 500
if (Boom.isBoom(err)) {
statusCode = err.output.statusCode
} else if (err.statusCode) {
statusCode = err.statusCode
}
// Since it's an error, it's safe to assume <400
// status codes are a mistake.
if (statusCode < 400) {
statusCode = 500
}
// Wrap the error and generate the response
const error = Boom.isBoom(err) ? err : Boom.boomify(err, { statusCode })
// Add WWW-Authenticate challenge to headers for 401 responses
if (statusCode === 401 && error.data && error.data.challenge) {
res.setHeader('WWW-Authenticate', error.data.challenge)
}
send(
res,
statusCode,
Object.assign({},
error.output.payload,
error.data && { data: error.data }
)
)
}
}
const negotiateError = (err) => {
if (_.isError(err)) {
if (!Boom.isBoom(err)) {
Boom.boomify(err, { statusCode: 403, override: false });
}
return err;
}
// In case someone throws a string or something equally terrifying
return Boom.forbidden(err);
};
if (settings.keepAlive) {
h.state(settings.name, session);
}
return h.authenticated({ credentials, artifacts: session });
}
catch (err) {
Bounce.rethrow(err, 'system');
if (settings.cookie.clearInvalid) {
h.unstate(settings.name);
}
const unauthorized = Boom.isBoom(err) && err.typeof === Boom.unauthorized ? err : Boom.unauthorized('Invalid cookie');
return unauthenticated(unauthorized, { credentials, artifacts: session });
}
};
app.use((err, req, res, next) => {
log.error(err, `error processing a request ${req.method} ${req.originalUrl}`)
if (boom.isBoom(err)) {
res.status(err.output.statusCode).end(err.output.payload.message)
} else {
res.status(500).end('Internal Server Error')
}
})
module.exports = (err, req, res, next) => {
if (Boom.isBoom(err)) {
if (err.output.statusCode === 422) {
return res.status(err.output.statusCode).json({
errors: err.data,
});
}
if (config.get('env') === 'production') {
return res.status(err.output.statusCode).json({
errors: {
message: err.message,
error: {},
},
});
}
req.log.error(err.stack);