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);
}- Name appears once - less editing if you have to create lots of custom error classes
- Easy to subclass - just change the last line to inherit from another custom error class you created
- 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 touchthisat allError.captureStackTrace(this, arguments.callee);- works, butarguments.calleeis deprecated, so don't use itthis.stack = (new Error).stack- this... I don't even...