Skip to content

Commit

Permalink
Respect name in stack trace.
Browse files Browse the repository at this point in the history
This makes sure to set the name before doing `Error.call(error)` and capturing
the stack trace so the user-provided name is reflected in the stack trace
instead of the generic `Error: ...`.
  • Loading branch information
brianloveswords committed Jul 14, 2012
1 parent 19dad0c commit d1756bb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/errs.js
Expand Up @@ -96,7 +96,7 @@ exports.create = function createErr(type, opts) {
error = new (ErrorProto)(message);

if (!error.name || error.name === 'Error') {
error.name = ErrorProto.name || 'Error';
error.name = (opts && opts.name) || ErrorProto.name || 'Error';
}

//
Expand Down
8 changes: 7 additions & 1 deletion test/errs-test.js
Expand Up @@ -45,7 +45,13 @@ vows.describe('errs').addBatch({
"no parameters": macros.create.string('An error as a string'),
"an object": {
"that has no message": macros.create.object(opts[0]),
"that has a message": macros.create.object(opts[1])
"that has a message": macros.create.object(opts[1]),
"that has a name": {
topic : errs.create({name: 'OverflowError'}),
"should respect that name in the stack trace" : function (err) {
assert.match(err.stack, /^OverflowError/);
},
}
},
"an error": macros.create.err(new Error('An instance of an error')),
"a function": macros.create.fn(function () {
Expand Down

0 comments on commit d1756bb

Please sign in to comment.