Skip to content

Commit

Permalink
support for custom prototypes for creating families
Browse files Browse the repository at this point in the history
  • Loading branch information
daaku committed Aug 8, 2011
1 parent 2343414 commit 2bfcc8e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
12 changes: 9 additions & 3 deletions lib/makeerror.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,18 @@ module.exports = makeError
* @param Object The default data object, merged with per instance data.
*/
function makeError(name, defaultMessage, defaultData) {
defaultMessage = defaultMessage || ''
defaultData = defaultData || {}

var CustomError = function(message, data) {
if (!(this instanceof CustomError)) return new CustomError(message, data)
this.name = name
this.message = message || defaultMessage || ''
this.data = data || defaultData || {}
this.message = message || defaultMessage
this.data = data || defaultData
}
CustomError.prototype = new Error()

CustomError.prototype = defaultData.proto || new Error()
delete defaultData.proto

return CustomError
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "makeerror",
"description": "A library to make errors.",
"version": "1.0.1",
"version": "1.0.2",
"author": "Naitik Shah <n@daaku.org>",
"main": "lib/makeerror",
"repository": {
Expand Down
33 changes: 33 additions & 0 deletions test/makeerror-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,36 @@ exports['different but the same'] = function(beforeExit) {
assert.ok(er2 instanceof MyError2, 'Must be an instance of MyError2')
assert.ok(!(er2 instanceof MyError1), 'Must not be an instance of MyError1')
}

exports['custom prototype'] = function(beforeExit) {
var parentName = 'ParentError'
, ParentError = makeError(parentName)
, childName = 'ChildError'
, ChildError = makeError(childName, '', { proto: ParentError() })
, parentEr = ParentError()
, childEr = ChildError()

assert.strictEqual(parentEr.name, parentName, 'expect parentName')
assert.ok(parentEr instanceof ParentError, 'expect instanceof ParentError')
assert.ok(parentEr instanceof Error, 'expect instanceof Error')
assert.ok(!(parentEr instanceof ChildError), 'not an instanceof ChildError')

assert.strictEqual(childEr.name, childName, 'expect childName')
assert.ok(childEr instanceof ChildError, 'expect instanceof ChildError')
assert.ok(childEr instanceof ParentError, 'expect instanceof ParentError')
assert.ok(childEr instanceof Error, 'expect instanceof Error')
}

exports['custom prototype two levels'] = function(beforeExit) {
var GParentError = makeError('GParentError')
, ParentError = makeError('ParentError', '', { proto: GParentError() })
, childName = 'ChildError'
, ChildError = makeError(childName, '', { proto: ParentError() })
, childEr = ChildError()

assert.strictEqual(childEr.name, childName, 'expect childName')
assert.ok(childEr instanceof ChildError, 'expect instanceof ChildError')
assert.ok(childEr instanceof ParentError, 'expect instanceof ParentError')
assert.ok(childEr instanceof GParentError, 'expect instanceof GParentError')
assert.ok(childEr instanceof Error, 'expect instanceof Error')
}

0 comments on commit 2bfcc8e

Please sign in to comment.