Skip to content

Commit 4b6c74b

Browse files
committed
Improve compatibility with + add test for DOMException
1 parent 059b44a commit 4b6c74b

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

implementation.js

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,41 @@ var $toString = callBound('Object.prototype.toString');
1414
var stackDesc = gOPD($Error.prototype, 'stack');
1515
var stackGetter = stackDesc && stackDesc.get && callBind(stackDesc.get);
1616

17+
var domExceptionClonable = !!($structuredClone && $structuredClone(new DOMException()) instanceof $Error);
18+
1719
module.exports = function isError(arg) {
1820
if (!arg || (typeof arg !== 'object' && typeof arg !== 'function')) {
1921
return false; // step 1
2022
}
2123

22-
if (isNativeError) { // node 10+
23-
return isNativeError(arg);
24-
}
25-
2624
if ($structuredClone) {
2725
try {
28-
return $structuredClone(arg) instanceof $Error;
26+
if ($structuredClone(arg) instanceof $Error) {
27+
return true;
28+
} else if (domExceptionClonable) {
29+
return false;
30+
}
2931
} catch (e) {
3032
return false;
3133
}
3234
}
3335

34-
if (!hasToStringTag || !(Symbol.toStringTag in arg)) {
35-
var str = $toString(arg);
36-
return str === '[object Error]' // errors
37-
|| str === '[object DOMException]' // browsers
38-
|| str === '[object DOMError]' // browsers, deprecated
39-
|| str === '[object Exception]'; // sentry
36+
if (isNativeError && isNativeError(arg)) { // node 10+
37+
return true;
38+
}
39+
40+
var str = $toString(arg);
41+
42+
if (
43+
str === '[object DOMException]' // browsers
44+
|| ((!hasToStringTag || !(Symbol.toStringTag in arg))
45+
&& (
46+
str === '[object DOMError]' // browsers, deprecated
47+
|| str === '[object Exception]' // sentry
48+
)
49+
)
50+
) {
51+
return true;
4052
}
4153

4254
// Firefox
@@ -56,5 +68,5 @@ module.exports = function isError(arg) {
5668
}
5769

5870
// fallback for envs with toStringTag but without structuredClone
59-
return arg instanceof Error;
71+
return arg instanceof Error || arg instanceof DOMException;
6072
};

test/tests.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ module.exports = function (isError, t) {
6060
new URIError(),
6161
new EvalError(),
6262
typeof AggregateError === 'function' ? new AggregateError([]) : [],
63-
typeof SuppressedError === 'function' ? new SuppressedError() : []
63+
typeof SuppressedError === 'function' ? new SuppressedError() : [],
64+
typeof DOMException === 'function' ? new DOMException() : []
6465
), function (error) {
6566
st.equal(isError(error), true, inspect(error) + ' is an Error object');
6667
});

0 commit comments

Comments
 (0)