-
Notifications
You must be signed in to change notification settings - Fork 0
/
isError.js
55 lines (48 loc) · 1.29 KB
/
isError.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* @file Detect whether a value is an error.
* @copyright Copyright (c) 2018-present, Graham Fairweather
* @module isError
*/
import _getPrototypeOf from './.internal/_getPrototypeOf';
import toStringTag from './toStringTag';
import isObjectLike from './isObjectLike';
let testIsError = function _testIsError(value) {
return toStringTag(value) === '[object Error]';
};
/* istanbul ignore next */
if (!testIsError(Error.prototype)) {
const errorProto = Error.prototype;
const testStringTag = testIsError;
testIsError = function _testIsError(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`.
* @example
* var isError = require('is-error-x');
*
* isError(); // false
* isError(Number.MIN_VALUE); // false
* isError('abc'); // false
* isError(new Error()); //true
*/
export default function isError(value) {
if (!isObjectLike(value)) {
return false;
}
let object = value;
let maxLoop = 100;
while (object && maxLoop > -1) {
if (testIsError(object)) {
return true;
}
object = _getPrototypeOf(object);
maxLoop -= 1;
}
return false;
}