basic-expressjs/utils/middleware/errorHandlers.js
2021-09-15 13:47:11 -05:00

39 lines
656 B
JavaScript

const boom = require('@hapi/boom');
const { config } = require('../../config');
function withErrorStack(error, stack) {
if (config.dev) {
return { ...error, stack }
}
return error;
}
function wrapErrors(err, req, res, next) {
if (!err.isBoom) {
next(boom.badImplementation(err));
}
next(err);
}
function logErrors(err, req, res, next) {
console.log(err);
next(err);
}
function errorHandler(err, req, res, next) {
const {
output: {
statusCode, payload
}
} = err;
res.status(statusCode || 500);
res.json(withErrorStack(payload, err.stack));
}
module.exports = {
logErrors,
wrapErrors,
errorHandler
}