From 684dd28a6c684532336777348875ac11305727b9 Mon Sep 17 00:00:00 2001 From: Rod Vagg Date: Tue, 15 Oct 2013 12:26:18 +1100 Subject: [PATCH] util: format as Error if instanceof Error --- lib/util.js | 3 ++- test/simple/test-util-format.js | 10 ++++++++++ test/simple/test-util.js | 2 +- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/util.js b/lib/util.js index 9901a66..b0bbdd4 100644 --- a/lib/util.js +++ b/lib/util.js @@ -496,7 +496,8 @@ function isDate(d) { exports.isDate = isDate; function isError(e) { - return isObject(e) && objectToString(e) === '[object Error]'; + return isObject(e) && + (objectToString(e) === '[object Error]' || e instanceof Error); } exports.isError = isError; diff --git a/test/simple/test-util-format.js b/test/simple/test-util-format.js index ee96744..1bea1bf 100644 --- a/test/simple/test-util-format.js +++ b/test/simple/test-util-format.js @@ -66,3 +66,13 @@ assert.equal(util.format('%%%s%%%%', 'hi'), '%hi%%'); o.o = o; assert.equal(util.format('%j', o), '[Circular]'); })(); + +// Errors +assert.equal(util.format(new Error('foo')), '[Error: foo]'); +function CustomError(msg) { + Error.call(this); + Object.defineProperty(this, 'message', { value: msg, enumerable: false }); + Object.defineProperty(this, 'name', { value: 'CustomError', enumerable: false }); +} +util.inherits(CustomError, Error); +assert.equal(util.format(new CustomError('bar')), '[CustomError: bar]'); diff --git a/test/simple/test-util.js b/test/simple/test-util.js index 2acdbdb..4e75d51 100644 --- a/test/simple/test-util.js +++ b/test/simple/test-util.js @@ -68,7 +68,7 @@ assert.equal(true, util.isError(new (context('SyntaxError')))); assert.equal(false, util.isError({})); assert.equal(false, util.isError({ name: 'Error', message: '' })); assert.equal(false, util.isError([])); -assert.equal(false, util.isError(Object.create(Error.prototype))); +assert.equal(true, util.isError(Object.create(Error.prototype))); // isObject assert.ok(util.isObject({}) === true); -- 2.7.4