4xx/5xx errors with their statusCode in it.
var BadRequestError = require('./http-errors').BadRequestError;
function doSomethingBad() {
throw new BadRequestError('It went bad!');
}
try {
doSomethingBad();
} catch (err) {
assert.strictEqual(err.statusCode, 400);
}
- 4xx/5xx status as errors classes - Status text is the class name, e.g. UnauthorizedError, NotFoundError
- Status code property - Error handlers can use the status code to send responses.
- Correct stack trace - no extra stack frames, no double capturing of the stack trace
These are some things that I've seen in other proposed solutions that you should avoid.
Error.call(this)
- creates another error object (wasting a bunch of time) and doesn't touchthis
at allError.captureStackTrace(this, arguments.callee);
- works, butarguments.callee
is deprecated, so don't use itthis.stack = (new Error).stack
- this... I don't even...