Open richardschneider opened 8 years ago
Sample middleware raising an error
function x(req, res, next) {
let error = new Error('"x" is not yet implemented');
error.status = 501; // HTTP status code
return next(error);
}
node-http-error provides us with capability to use a single line to return an error.
let HttpError = require('node-http-error');
function x(req, res, next) {
return next(new HttpError (501, '"x" is not yet implemented'));
}
Middleware should be as agnostic as possible. Errors should be raised by calling
next(err)
and NOT callingres.sendError(...)
.See express error handling for more details.