richardschneider / yappy

Yet another REST API server howling at the moon with JSON
MIT License
2 stars 0 forks source link

Middleware errors #153

Open richardschneider opened 8 years ago

richardschneider commented 8 years ago

Middleware should be as agnostic as possible. Errors should be raised by calling next(err) and NOT calling res.sendError(...).

See express error handling for more details.

richardschneider commented 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);
}
richardschneider commented 8 years ago

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'));
}