express-err
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -19,3 +19,3 @@ var util = require('util'); | ||
function httpErrorFactory(status, message) { | ||
return function httpError(req, res, next) { | ||
return function httpErrorMiddleware(req, res, next) { | ||
next(new HttpError(status, message)); | ||
@@ -33,7 +33,14 @@ }; | ||
function HttpError(status, message) { | ||
Error.call(this); | ||
this.status = status; | ||
// Import everything needed into 'this' so it "duck type" an Error object. | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#Custom_Error_Types | ||
this.name = 'HttpError'; | ||
this.message = message || statusCodes[status]; | ||
this.stack = (new Error()).stack; | ||
this.status = status; // non-standard : hint at http status code | ||
} | ||
util.inherits(HttpError, Error); | ||
// REM "Error" always returns a new object | ||
// cf. http://www.ecma-international.org/ecma-262/5.1/#sec-15.11.1 | ||
HttpError.prototype = Object.create(Error.prototype); | ||
HttpError.prototype.constructor = HttpError; |
{ | ||
"name": "express-err", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Basic error handler for express.", | ||
@@ -34,4 +34,4 @@ "main": "index.js", | ||
"sinon-chai": "^2.7.0", | ||
"supertest": "^0.14.0" | ||
"supertest": "^0.15.0" | ||
} | ||
} |
@@ -10,4 +10,6 @@ var express = require('express'); | ||
var error = new HttpError(401, 'my message'); | ||
expect(error).to.have.property('name', 'HttpError'); | ||
expect(error).to.have.property('message', 'my message'); | ||
expect(error).to.have.property('stack'); | ||
expect(error).to.have.property('status', 401); | ||
expect(error).to.have.property('message', 'my message'); | ||
}); | ||
@@ -17,4 +19,6 @@ | ||
var error = new HttpError(404); | ||
expect(error).to.have.property('name', 'HttpError'); | ||
expect(error).to.have.property('message', 'Not Found'); | ||
expect(error).to.have.property('stack'); | ||
expect(error).to.have.property('status', 404); | ||
expect(error).to.have.property('message', 'Not Found'); | ||
}); | ||
@@ -35,4 +39,6 @@ }); | ||
expect(err).to.be.instanceof(Error); | ||
expect(err).to.have.property('name', 'HttpError'); | ||
expect(err).to.have.property('message', 'Not Found'); | ||
expect(err).to.have.property('stack'); | ||
expect(err).to.have.property('status', 404); | ||
expect(err).to.have.property('message', 'Not Found'); | ||
done(); | ||
@@ -49,4 +55,6 @@ }); | ||
expect(err).to.be.instanceof(Error); | ||
expect(err).to.have.property('name', 'HttpError'); | ||
expect(err).to.have.property('message', 'Custom error message.'); | ||
expect(err).to.have.property('stack'); | ||
expect(err).to.have.property('status', 408); | ||
expect(err).to.have.property('message', 'Custom error message.'); | ||
done(); | ||
@@ -53,0 +61,0 @@ }); |
15006
343