Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Commit 684dd28

Browse files
rvaggbnoordhuis
authored andcommitted
util: format as Error if instanceof Error
1 parent 45885a1 commit 684dd28

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

lib/util.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,8 @@ function isDate(d) {
496496
exports.isDate = isDate;
497497

498498
function isError(e) {
499-
return isObject(e) && objectToString(e) === '[object Error]';
499+
return isObject(e) &&
500+
(objectToString(e) === '[object Error]' || e instanceof Error);
500501
}
501502
exports.isError = isError;
502503

test/simple/test-util-format.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,13 @@ assert.equal(util.format('%%%s%%%%', 'hi'), '%hi%%');
6666
o.o = o;
6767
assert.equal(util.format('%j', o), '[Circular]');
6868
})();
69+
70+
// Errors
71+
assert.equal(util.format(new Error('foo')), '[Error: foo]');
72+
function CustomError(msg) {
73+
Error.call(this);
74+
Object.defineProperty(this, 'message', { value: msg, enumerable: false });
75+
Object.defineProperty(this, 'name', { value: 'CustomError', enumerable: false });
76+
}
77+
util.inherits(CustomError, Error);
78+
assert.equal(util.format(new CustomError('bar')), '[CustomError: bar]');

test/simple/test-util.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ assert.equal(true, util.isError(new (context('SyntaxError'))));
6868
assert.equal(false, util.isError({}));
6969
assert.equal(false, util.isError({ name: 'Error', message: '' }));
7070
assert.equal(false, util.isError([]));
71-
assert.equal(false, util.isError(Object.create(Error.prototype)));
71+
assert.equal(true, util.isError(Object.create(Error.prototype)));
7272

7373
// isObject
7474
assert.ok(util.isObject({}) === true);

0 commit comments

Comments
 (0)