Skip to content

Commit

Permalink
Improve compatibility with + add test for DOMException
Browse files Browse the repository at this point in the history
  • Loading branch information
lionel-rowe committed Nov 13, 2024
1 parent 059b44a commit 4b6c74b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
36 changes: 24 additions & 12 deletions implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,41 @@ var $toString = callBound('Object.prototype.toString');
var stackDesc = gOPD($Error.prototype, 'stack');
var stackGetter = stackDesc && stackDesc.get && callBind(stackDesc.get);

var domExceptionClonable = !!($structuredClone && $structuredClone(new DOMException()) instanceof $Error);

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;
if ($structuredClone(arg) instanceof $Error) {
return true;
} else if (domExceptionClonable) {
return false;
}
} 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
if (isNativeError && isNativeError(arg)) { // node 10+
return true;
}

var str = $toString(arg);

if (
str === '[object DOMException]' // browsers
|| ((!hasToStringTag || !(Symbol.toStringTag in arg))
&& (
str === '[object DOMError]' // browsers, deprecated
|| str === '[object Exception]' // sentry
)
)
) {
return true;
}

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

// fallback for envs with toStringTag but without structuredClone
return arg instanceof Error;
return arg instanceof Error || arg instanceof DOMException;
};
3 changes: 2 additions & 1 deletion test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ module.exports = function (isError, t) {
new URIError(),
new EvalError(),
typeof AggregateError === 'function' ? new AggregateError([]) : [],
typeof SuppressedError === 'function' ? new SuppressedError() : []
typeof SuppressedError === 'function' ? new SuppressedError() : [],
typeof DOMException === 'function' ? new DOMException() : []
), function (error) {
st.equal(isError(error), true, inspect(error) + ' is an Error object');
});
Expand Down

0 comments on commit 4b6c74b

Please sign in to comment.