util.isError()

util.isError(object)

Stability: 0 - Deprecated

Returns true if the given "object" is an Error. Otherwise, returns false.

1
2
3
4
5
6
7
8
const util = require('util');
 
util.isError(new Error())
  // true
util.isError(new TypeError())
  // true
util.isError({ name: 'Error', message: 'an error occurred' })
  // false

Note that this method relies on Object.prototype.toString() behavior. It is possible to obtain an incorrect result when the object argument manipulates @@toStringTag.

1
2
3
4
5
6
7
8
9
// This example requires the `--harmony-tostring` flag
const util = require('util');
const obj = { name: 'Error', message: 'an error occurred' };
 
util.isError(obj);
  // false
obj[Symbol.toStringTag] = 'Error';
util.isError(obj);
  // true
doc_Nodejs
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.