From d1756bbf93706ab6a56334e59f7b3e39b31730bb Mon Sep 17 00:00:00 2001 From: Brian J Brennan Date: Sat, 14 Jul 2012 12:34:23 -0400 Subject: [PATCH] Respect name in stack trace. 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: ...`. --- lib/errs.js | 2 +- test/errs-test.js | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/errs.js b/lib/errs.js index ea30d56..462c65b 100644 --- a/lib/errs.js +++ b/lib/errs.js @@ -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'; } // diff --git a/test/errs-test.js b/test/errs-test.js index 4178f49..8363d85 100644 --- a/test/errs-test.js +++ b/test/errs-test.js @@ -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 () {