Ghost/core/server/middleware/api-error-handlers.js
Hannah Wolfe 254e0f0597 Improve API error handling
close #2757, refs #5286

- moves error formatting from api/index into errors lib
- moves error handling from api/index into its own middleware
- adds extra middleware for method not allowed which captures all unsupported routes
2015-06-15 10:08:30 +01:00

14 lines
519 B
JavaScript

var errors = require('../errors');
module.exports.methodNotAllowed = function methodNotAllowed(req, res, next) {
next(new errors.MethodNotAllowedError('Unknown method: ' + req.path));
};
module.exports.errorHandler = function errorHandler(err, req, res, next) {
/*jshint unused:false */
var httpErrors = errors.formatHttpErrors(err);
errors.logError(err);
// Send a properly formatted HTTP response containing the errors
res.status(httpErrors.statusCode).json({errors: httpErrors.errors});
};