Skip to content

Commit

Permalink
util: format as Error if instanceof Error
Browse files Browse the repository at this point in the history
Conflicts:
	lib/util.js
	test/simple/test-util-format.js

This is a backport to fix nodejs#7253

Signed-off-by: Fedor Indutny <[email protected]>
  • Loading branch information
rvagg authored and indutny committed Apr 25, 2014
1 parent f9ced08 commit 250782d
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 @@ -452,7 +452,8 @@ exports.isDate = isDate;


function isError(e) {
return typeof e === 'object' && objectToString(e) === '[object Error]';
return typeof e === 'object' &&
(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 @@ -60,3 +60,13 @@ assert.equal(util.format('%s:%s', 'foo', 'bar'), 'foo:bar');
assert.equal(util.format('%s:%s', 'foo', 'bar', 'baz'), 'foo:bar baz');
assert.equal(util.format('%%%s%%', 'hi'), '%hi%');
assert.equal(util.format('%%%s%%%%', 'hi'), '%hi%%');

// 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)));

// _extend
assert.deepEqual(util._extend({a:1}), {a:1});
Expand Down

0 comments on commit 250782d

Please sign in to comment.