-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
implementation.js
60 lines (49 loc) · 1.46 KB
/
implementation.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
56
57
58
59
60
'use strict';
var $Error = require('es-errors');
var hasToStringTag = require('has-tostringtag')();
var callBind = require('call-bind');
var callBound = require('call-bind/callBound');
var gOPD = require('gopd');
var isNativeError = require('./helpers/isNativeError');
var $structuredClone = typeof structuredClone === 'function' ? structuredClone : null;
var $toString = callBound('Object.prototype.toString');
var stackDesc = gOPD($Error.prototype, 'stack');
var stackGetter = stackDesc && stackDesc.get && callBind(stackDesc.get);
module.exports = function isError(arg) {
if (!arg || (typeof arg !== 'object' && typeof arg !== 'function')) {
return false; // step 1
}
if (isNativeError) { // node 10+
return isNativeError(arg);
}
if ($structuredClone) {
try {
return $structuredClone(arg) instanceof $Error;
} catch (e) {
return false;
}
}
if (!hasToStringTag || !(Symbol.toStringTag in arg)) {
var str = $toString(arg);
return str === '[object Error]' // errors
|| str === '[object DOMException]' // browsers
|| str === '[object DOMError]' // browsers, deprecated
|| str === '[object Exception]'; // sentry
}
// Firefox
if (stackGetter) {
try {
stackGetter(arg);
return true;
} catch (e) {
return false;
}
}
/* globals DOMError */
if (typeof DOMError !== 'undefined' && arg instanceof DOMError) {
// Edge 80
return true;
}
// fallback for envs with toStringTag but without structuredClone
return arg instanceof Error;
};