isErrorRaw
Returns true
if the given object
is an Error
. Otherwise, returns false
.
const util = require('node:util');
util.isError(new Error());
// Returns: true
util.isError(new TypeError());
// Returns: true
util.isError({ name: 'Error', message: 'an error occurred' });
// Returns: false
Content copied to clipboard
This method relies on Object.prototype.toString()
behavior. It is possible to obtain an incorrect result when the object
argument manipulates @@toStringTag
.
const util = require('node:util');
const obj = { name: 'Error', message: 'an error occurred' };
util.isError(obj);
// Returns: false
obj[Symbol.toStringTag] = 'Error';
util.isError(obj);
// Returns: true
Content copied to clipboard
Since
v0.6.0