Skip to content

Commit

Permalink
util: format as Error if instanceof Error
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg authored and bnoordhuis committed Oct 15, 2013
1 parent 45885a1 commit 684dd28
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
10 changes: 10 additions & 0 deletions test/simple/test-util-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]');
2 changes: 1 addition & 1 deletion test/simple/test-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 684dd28

Please sign in to comment.