createerror
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -40,45 +40,38 @@ (function (root, factory) { | ||
options = options || {}; | ||
SuperConstructor = SuperConstructor || Error; | ||
function Constructor(message) { | ||
function Constructor(messageOrOptionsOrError) { | ||
var err; | ||
if (message instanceof Error) { | ||
err = new Constructor(); | ||
err.stack = message.stack; | ||
extend(err, message); | ||
return err; | ||
if (messageOrOptionsOrError instanceof Error) { | ||
// Existing instance | ||
err = messageOrOptionsOrError; | ||
} else { | ||
if (this instanceof Constructor) { | ||
err = this; | ||
} else { | ||
// Can't use `return new Constructor(message)` here as that would introduce an annoying extra frame in err.stack. | ||
err = new Constructor(message); | ||
var message = typeof messageOrOptionsOrError === 'string' ? messageOrOptionsOrError : options.message; | ||
// https://github.com/joyent/node/issues/3212#issuecomment-5493890 | ||
err = new Error(message); | ||
err.__proto__ = Constructor.prototype; | ||
if (Error.captureStackTrace) { | ||
Error.captureStackTrace(err, Constructor); | ||
} | ||
SuperConstructor.call(err); | ||
Error.captureStackTrace(err, Constructor); | ||
} | ||
if (typeof message === 'string') { | ||
err.message = message; | ||
} else if (typeof message === 'object' && message) { | ||
extend(err, message); | ||
if (SuperConstructor) { | ||
SuperConstructor.call(err, err); | ||
} | ||
extend(err, options); | ||
if (typeof messageOrOptionsOrError === 'object' && messageOrOptionsOrError) { | ||
extend(err, messageOrOptionsOrError); | ||
} | ||
return err; | ||
}; | ||
inherits(Constructor, SuperConstructor); | ||
inherits(Constructor, SuperConstructor || Error); | ||
// to avoid doing if (err instanceof NotFound) | ||
// instead you can just do if (err.NotFound) | ||
options[options.name] = true; | ||
if (options.name) { | ||
Constructor.name = options.name; | ||
options[options.name] = true; | ||
} | ||
extend(Constructor.prototype, options); | ||
Constructor.prototype.toString = function () { | ||
return this.name + | ||
(this.statusCode ? ' [' + this.statusCode + ']' : '') + | ||
(this.message ? ': ' + this.message : ''); | ||
}; | ||
return Constructor; | ||
}; | ||
})); |
@@ -6,3 +6,3 @@ { | ||
"keywords": ["errors", "error", "class", "errorclass"], | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"repository": { | ||
@@ -9,0 +9,0 @@ "type": "git", |
@@ -5,2 +5,35 @@ var expect = require('unexpected'), | ||
describe('createError', function () { | ||
// https://github.com/joyent/node/issues/3212 | ||
it('should return "[object Error]" when Object.prototype.toString is called on it', function () { | ||
var Err = createError({foo: 'bar'}); | ||
expect(Object.prototype.toString.call(new Err()), 'to equal', '[object Error]'); | ||
}); | ||
it('should result a constructor that produces instances of itself and Error', function () { | ||
var Err = createError({foo: 'bar'}); | ||
expect(new Err(), 'to be an', Err); | ||
}); | ||
it('should return a constructor that produces instances of its SuperConstructor and Error', function () { | ||
var SuperErr = createError({quux: 'baz'}), | ||
Err = createError({foo: 'bar'}, SuperErr); | ||
expect(new Err(), 'to be an', Error); | ||
expect(new Err(), 'to be a', SuperErr); | ||
}); | ||
it('should return a constructor that produces instances whose toString method return the expected value', function () { | ||
var Err = createError({foo: 'bar', name: 'TheErrorName'}); | ||
expect(new Err('error message').toString(), 'to equal', 'TheErrorName: error message'); | ||
expect(new Err().toString(), 'to equal', 'TheErrorName'); | ||
}); | ||
it('should subclass TypeError (and thus other built-in Error subclasses) correctly', function () { | ||
var Err = createError({foo: 'bar', name: 'TheErrorName'}, TypeError), | ||
err = new Err('the error message'); | ||
expect(Object.prototype.toString.call(err), 'to equal', '[object Error]'); | ||
expect(err, 'to be a', TypeError); | ||
expect(err, 'to be an', Error); | ||
expect(err, 'to be an', Err); | ||
}); | ||
it('should adopt arbitrary properties passed to a generated constructor', function () { | ||
@@ -22,3 +55,3 @@ var Err = createError({foo: 'bar'}), | ||
} | ||
var SuperError = createError({isSuper: true}), | ||
var SuperError = createError({isSuper: true, name: 'Super'}), | ||
Err = createError({name: 'SomethingMoreSpecific'}, SuperError), | ||
@@ -25,0 +58,0 @@ err = new Err(Foo('blabla')); |
9553
129