Design & Download
Your JS Library
in Seconds!!!

Snippet: isError()

Written by Christopher West (cwest) on August 08, 2016.
Determines if something is an Error object.
function isError(obj, opt_testForAnyError) {
  return typeOf(obj).slice(opt_testForAnyError ? -5 : 0) == 'Error';
}

isError(obj) API Documentation

Description

Determines if something is an Error object.

Parameters

  1. obj {*}:
    The value to be checked.
  2. opt_testForAnyError {boolean}:
    Optional. Defaults to false. Determines whether or not the function will check to see if obj is any type of Error object or only the basic Error object.

Returns

Returns true if obj is an Error object, otherwise false is returned. If opt_testForAnyError is true then true will be returned as long as YourJS.typeOf(obj) ends in "Error".

Example

console.log(YourJS.isError(new Error));  // -> true
console.log(YourJS.isError(new SyntaxError));  // -> false

console.log(YourJS.isError(new SyntaxError, true));  // -> true
console.log(YourJS.isError(new TypeError, true));  // -> true
console.log(YourJS.isError(new ReferenceError, true));  // -> true
console.log(YourJS.isError(new URIError, true));  // -> true
console.log(YourJS.isError(new RangeError, true));  // -> true
console.log(YourJS.isError(new EvalError, true));  // -> true