util: format as Error if instanceof Error
This commit is contained in:
parent
45885a1e8c
commit
684dd28a6c
@ -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;
|
||||
|
||||
|
@ -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]');
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user