// Mini test suite for our 4xx/5xx error var assert = require('assert'); var BadRequestError = require('./http-errors').BadRequestError function doSomethingBad() { throw new BadRequestError('It went bad!'); } try { doSomethingBad(); } catch (err) { // The name property should be set to the error's name assert(err.name = 'BadRequestError'); // The error should be an instance of its class assert(err instanceof BadRequestError); // The error should be an instance of builtin Error assert(err instanceof Error); // The error should be recognized by Node.js' util#isError assert(require('util').isError(err)); // The error should have recorded a stack assert(err.stack); // toString should return the default error message formatting assert.strictEqual(err.toString(), 'BadRequestError: It went bad!'); // The stack should start with the default error message formatting assert.strictEqual(err.stack.split('\n')[0], 'BadRequestError: It went bad!'); // The first stack frame should be the function where the error was thrown. assert.strictEqual(err.stack.split('\n')[1].indexOf('doSomethingBad'), 7); // The extra property should have been set assert.strictEqual(err.statusCode, 400); }