Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve compatibility with + add test for DOMException #3

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 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);
lionel-rowe marked this conversation as resolved.
Show resolved Hide resolved

module.exports = function isError(arg) {
if (!arg || (typeof arg !== 'object' && typeof arg !== 'function')) {
ljharb marked this conversation as resolved.
Show resolved Hide resolved
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
ljharb marked this conversation as resolved.
Show resolved Hide resolved
|| str === '[object Exception]' // sentry
lionel-rowe marked this conversation as resolved.
Show resolved Hide resolved
)
)
) {
return true;
}

// Firefox
Expand Down
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
Loading