Skip to content

Commit

Permalink
fix(browser): Capture stacktrace on DOMExceptions, if possible (#4160)
Browse files Browse the repository at this point in the history
Inspired by #4156 - h/t @nowylie.

According to the spec[1], all `DOMExceptions` should also be `Error`s (and therefore potentially have a stacktrace), though this isn't universally followed. In the cases in which it is, we should be capturing that stack.

Fixes #4085
Fixes #3119

[1] https://webidl.spec.whatwg.org/#es-DOMException-specialness
  • Loading branch information
lobsterkatie authored Nov 15, 2021
1 parent fb66a78 commit c886062
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions packages/browser/src/eventbuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,25 @@ export function eventFromUnknownInput(
event = eventFromStacktrace(computeStackTrace(exception as Error));
return event;
}

// If it is a `DOMError` (which is a legacy API, but still supported in some browsers) then we just extract the name
// and message, as it doesn't provide anything else. According to the spec, all `DOMExceptions` should also be
// `Error`s, but that's not the case in IE11, so in that case we treat it the same as we do a `DOMError`.
//
// https://developer.mozilla.org/en-US/docs/Web/API/DOMError
// https://developer.mozilla.org/en-US/docs/Web/API/DOMException
// https://webidl.spec.whatwg.org/#es-DOMException-specialness
if (isDOMError(exception as DOMError) || isDOMException(exception as DOMException)) {
// If it is a DOMError or DOMException (which are legacy APIs, but still supported in some browsers)
// then we just extract the name, code, and message, as they don't provide anything else
// https://developer.mozilla.org/en-US/docs/Web/API/DOMError
// https://developer.mozilla.org/en-US/docs/Web/API/DOMException
const domException = exception as DOMException;
const name = domException.name || (isDOMError(domException) ? 'DOMError' : 'DOMException');
const message = domException.message ? `${name}: ${domException.message}` : name;

event = eventFromString(message, syntheticException, options);
addExceptionTypeValue(event, message);
if ('stack' in (exception as Error)) {
event = eventFromStacktrace(computeStackTrace(exception as Error));
} else {
const name = domException.name || (isDOMError(domException) ? 'DOMError' : 'DOMException');
const message = domException.message ? `${name}: ${domException.message}` : name;
event = eventFromString(message, syntheticException, options);
addExceptionTypeValue(event, message);
}
if ('code' in domException) {
event.tags = { ...event.tags, 'DOMException.code': `${domException.code}` };
}
Expand Down

0 comments on commit c886062

Please sign in to comment.