43 lines
998 B
JavaScript
43 lines
998 B
JavaScript
'use strict';
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// Error handlers
|
|
// -----------------------------------------------------------------------------
|
|
|
|
const container = require('../Container');
|
|
const app = container.get('app');
|
|
const HTTP_CODE_MAP = require('http').STATUS_CODES;
|
|
const errors = require('errors');
|
|
|
|
let errorHandlers = new Set([
|
|
|
|
function handle404(req, res, next) {
|
|
// if no route matches, send a 404
|
|
if (! req.route) {
|
|
let err = new errors.Http404Error();
|
|
return next(err);
|
|
}
|
|
},
|
|
|
|
// general error handler
|
|
function handleError(err, req, res, next) {
|
|
let httpStatus = err.status || 500;
|
|
let message = err.message || HTTP_CODE_MAP[httpStatus];
|
|
|
|
res.status(httpStatus);
|
|
|
|
let output = {
|
|
status: httpStatus,
|
|
message: message,
|
|
};
|
|
|
|
// Show stack trace in development environment
|
|
if (app.get('env') === 'development') {
|
|
output.error = err;
|
|
}
|
|
|
|
res.json(output);
|
|
},
|
|
]);
|
|
|
|
module.exports = errorHandlers; |