I have tried:
app.get('/', function(req, res, next) {
var e = new Error('error message');
e.status = 400;
next(e);
});
and:
app.get('/', function(req, res, next) {
res.statusCode = 400;
var e = new Error('error message');
next(e);
});
but always an error code of 500 is announced.
I would recommend handling the sending of http error codes by using the Boom package.
I'd like to centralize the creation of the error response in this way:
So I have always the same error output format.
PS: of course you could create an object to extend the standard error like this:
The version of the errorHandler middleware bundled with some (perhaps older?) versions of express seems to have the status code hardcoded. The version documented here: http://www.senchalabs.org/connect/errorHandler.html on the other hand lets you do what you are trying to do. So, perhaps trying upgrading to the latest version of express/connect.
express deprecated res.send(body, status). Use res.status(status).send(body) instead
A simple one liner;
Old question, but still coming up on Google. In the current version of Express (3.4.0), you can alter res.statusCode before calling next(err):