This repository has been archived on 2018-10-12. You can view files and clone it, but cannot push or open issues or pull requests.
crispy-train/app/config/error-handlers.js

43 lines
998 B
JavaScript
Raw Normal View History

2016-01-25 09:19:28 -05:00
'use strict';
// -----------------------------------------------------------------------------
// Error handlers
// -----------------------------------------------------------------------------
const container = require('../Container');
const app = container.get('app');
2016-02-18 21:50:45 -05:00
const HTTP_CODE_MAP = require('http').STATUS_CODES;
const errors = require('errors');
2016-01-25 09:19:28 -05:00
2016-02-18 21:50:45 -05:00
let errorHandlers = new Set([
2016-01-25 09:19:28 -05:00
2016-02-18 21:50:45 -05:00
function handle404(req, res, next) {
// if no route matches, send a 404
if (! req.route) {
let err = new errors.Http404Error();
return next(err);
}
2016-01-25 09:19:28 -05:00
},
// general error handler
2016-02-18 21:50:45 -05:00
function handleError(err, req, res, next) {
let httpStatus = err.status || 500;
let message = err.message || HTTP_CODE_MAP[httpStatus];
res.status(httpStatus);
2016-01-25 09:19:28 -05:00
let output = {
2016-02-18 21:50:45 -05:00
status: httpStatus,
message: message,
2016-01-25 09:19:28 -05:00
};
// Show stack trace in development environment
if (app.get('env') === 'development') {
output.error = err;
}
res.json(output);
},
2016-02-18 21:50:45 -05:00
]);
2016-01-25 09:19:28 -05:00
module.exports = errorHandlers;