httperrors
Advanced tools
Comparing version 0.1.1 to 0.2.0
36
index.js
var http = require('http'), | ||
util = require('util'), | ||
xtend = require('xtend'), | ||
createError = require('createerror'), | ||
httpErrors = module.exports; | ||
httpErrors.createError = function (options, SuperConstructor) { | ||
SuperConstructor = SuperConstructor || Error; | ||
function Constructor(message) { | ||
SuperConstructor.call(this); | ||
Error.captureStackTrace(this, arguments.callee); | ||
if (typeof message === 'string') { | ||
this.message = message; | ||
} else if (typeof message === 'object' && message) { | ||
xtend(this, message); | ||
} | ||
}; | ||
util.inherits(Constructor, SuperConstructor); | ||
xtend(Constructor.prototype, options); | ||
// to avoid doing if (err instanceof NotFound) | ||
// instead you can just do if (err.NotFound) | ||
Constructor.prototype[options.name] = true; | ||
Constructor.prototype.toString = function () { | ||
return this.name + | ||
(this.statusCode ? ' [' + this.statusCode + ']' : '') + | ||
(this.message ? ': ' + this.message : ''); | ||
}; | ||
return Constructor; | ||
}; | ||
/// Map the error codes/names, as defined in Node's [http | ||
/// module](http://nodejs.org/docs/latest/api/http.html). | ||
Object.keys(http.STATUS_CODES).forEach(function(statusCode) { | ||
Object.keys(http.STATUS_CODES).forEach(function (statusCode) { | ||
statusCode = +statusCode; // turn into a number | ||
@@ -55,3 +25,3 @@ var httpErrorName = http.STATUS_CODES[statusCode]; | ||
// allows for new httpErrors[res.statusCode] in a http proxying setting | ||
httpErrors[name] = httpErrors[statusCode] = httpErrors.createError({ | ||
httpErrors[name] = httpErrors[statusCode] = createError({ | ||
statusCode: statusCode, | ||
@@ -58,0 +28,0 @@ name: name |
@@ -6,3 +6,3 @@ { | ||
"keywords": ["http", "errors", "error", "request", "proxy", "client", "class", "errorclass"], | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"repository": { | ||
@@ -17,3 +17,3 @@ "type": "git", | ||
"dependencies": { | ||
"xtend": "1.0.3" | ||
"createerror": "0.0.1" | ||
}, | ||
@@ -20,0 +20,0 @@ "scripts": { |
node-httperrors | ||
=============== | ||
Exposes HTTP 4xx and 5xx status codes as JavaScript Error objects. | ||
Exposes HTTP 4xx and 5xx status codes as JavaScript Error objects. The error classes are created using the <a href="https://github.com/One-com/node-createerror">createError module</a>. | ||
The original use case for `httpErrors` is to use a custom <a href="https://github.com/visionmedia/express">express</a> error handler that uses the `statusCode` property of the error instance as the status code for the response, and optionally logs further info from the error. | ||
Installation | ||
@@ -27,4 +30,5 @@ ------------ | ||
The error type is exposed as a true property on the instances, so your error handling | ||
code becomes quite readable (and you can avoid using instanceof): | ||
The CamelCased error name is exposed as a true property on the | ||
instances, so your error handling code becomes quite readable (and you | ||
can avoid using instanceof): | ||
@@ -49,39 +53,2 @@ if (err.NotFound) { | ||
Creating your own Error classes: | ||
var httpErrors = require('httperrors'); | ||
var MyError = httpErrors.createError({ | ||
type: 'MyError', | ||
// Used when no message is handed to the constructor: | ||
message: 'A slightly longer description of the error' | ||
}); | ||
Instances can carry extra data about the error: | ||
try { | ||
throw new httpErrors.Forbidden({ | ||
message: "The message", // Not mandatory | ||
data: {disallowedIds: [1, 3, 4, 6]} | ||
}); | ||
} catch(e) { | ||
console.warn(e.data); // {disallowedIds: [1, 3, 4, 6]} | ||
} | ||
Inheriting from an existing Error class: | ||
var httpErrors = require('httperrors'); | ||
var NotFoundUnderTheBedError = httpErrors.createError({ | ||
type: 'NotFoundUnderTheBed', | ||
message: 'I looked under the bed, but it was not found' | ||
}, httpErrors.NotFound); | ||
Instances of this error walk and quack like `httpErrors.NotFound` instances, of course: | ||
var ohDear = new NotFoundUnderTheBedError('No monsters today'); | ||
console.warn(ohDear.NotFound); // true | ||
console.warn(ohDear.NotFoundUnderTheBed); // true | ||
License | ||
@@ -88,0 +55,0 @@ ------- |
24
test.js
@@ -0,10 +1,6 @@ | ||
var assert = require('assert'), | ||
httpErrors = require('./'); | ||
// builtin | ||
var assert = require('assert'); | ||
// local | ||
var errors = require('./'); | ||
test('NotFound', function() { | ||
var err = new errors.NotFound('sadface'); | ||
var err = new httpErrors.NotFound('sadface'); | ||
@@ -17,3 +13,3 @@ assert.equal(err.statusCode, 404); | ||
test('BadGateway', function() { | ||
var err = new errors.BadGateway(); | ||
var err = new httpErrors.BadGateway(); | ||
@@ -25,3 +21,3 @@ assert.equal(err.statusCode, 502); | ||
test('constructor with object', function() { | ||
var err = new errors.NotFound({ | ||
var err = new httpErrors.NotFound({ | ||
message: 'foo', | ||
@@ -34,11 +30,1 @@ url: 'bar' | ||
}); | ||
test('arbitrary property passed to createError', function() { | ||
var Err = errors.createError({foo: 'bar'}, errors.NotFound), | ||
err = new Err(); | ||
assert.equal(err.foo, 'bar'); | ||
var err2 = new Err({foo: 'quux'}); | ||
assert.equal(err2.foo, 'quux'); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5656
46
56
+ Addedcreateerror@0.0.1
+ Addedcreateerror@0.0.1(transitive)
- Removedxtend@1.0.3