util.isError()

util.isError(object)

Stability: 0 - Deprecated

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

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.

// 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
2016-04-30 04:43:04
Comments
Leave a Comment

Please login to continue.