UNPKG

is-error-x

Version:

Detect whether a value is an error.

48 lines (37 loc) 1.06 kB
import toStringTag from 'to-string-tag-x'; import isObjectLike from 'is-object-like-x'; import $getPrototypeOf from 'get-prototype-of-x'; var errorCheck = function checkIfError(value) { return toStringTag(value) === '[object Error]'; }; if (errorCheck(Error.prototype) === false) { var errorProto = Error.prototype; var testStringTag = errorCheck; errorCheck = function checkIfError(value) { return value === errorProto || testStringTag(value); }; } /** * Determine whether or not a given `value` is an `Error` type. * * @param {*} value - The object to be tested. * @returns {boolean} Returns `true` if `value` is an `Error` type, * else `false`. */ var isError = function isError(value) { if (isObjectLike(value) === false) { return false; } var object = value; var maxLoop = 100; while (object && maxLoop > -1) { if (errorCheck(object)) { return true; } object = $getPrototypeOf(object); maxLoop -= 1; } return false; }; export default isError; //# sourceMappingURL=is-error-x.esm.js.map