Skip to content

Commit c886062

Browse files
authored
fix(browser): Capture stacktrace on DOMExceptions, if possible (#4160)
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
1 parent fb66a78 commit c886062

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

packages/browser/src/eventbuilder.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,25 @@ export function eventFromUnknownInput(
7373
event = eventFromStacktrace(computeStackTrace(exception as Error));
7474
return event;
7575
}
76+
77+
// If it is a `DOMError` (which is a legacy API, but still supported in some browsers) then we just extract the name
78+
// and message, as it doesn't provide anything else. According to the spec, all `DOMExceptions` should also be
79+
// `Error`s, but that's not the case in IE11, so in that case we treat it the same as we do a `DOMError`.
80+
//
81+
// https://developer.mozilla.org/en-US/docs/Web/API/DOMError
82+
// https://developer.mozilla.org/en-US/docs/Web/API/DOMException
83+
// https://webidl.spec.whatwg.org/#es-DOMException-specialness
7684
if (isDOMError(exception as DOMError) || isDOMException(exception as DOMException)) {
77-
// If it is a DOMError or DOMException (which are legacy APIs, but still supported in some browsers)
78-
// then we just extract the name, code, and message, as they don't provide anything else
79-
// https://developer.mozilla.org/en-US/docs/Web/API/DOMError
80-
// https://developer.mozilla.org/en-US/docs/Web/API/DOMException
8185
const domException = exception as DOMException;
82-
const name = domException.name || (isDOMError(domException) ? 'DOMError' : 'DOMException');
83-
const message = domException.message ? `${name}: ${domException.message}` : name;
8486

85-
event = eventFromString(message, syntheticException, options);
86-
addExceptionTypeValue(event, message);
87+
if ('stack' in (exception as Error)) {
88+
event = eventFromStacktrace(computeStackTrace(exception as Error));
89+
} else {
90+
const name = domException.name || (isDOMError(domException) ? 'DOMError' : 'DOMException');
91+
const message = domException.message ? `${name}: ${domException.message}` : name;
92+
event = eventFromString(message, syntheticException, options);
93+
addExceptionTypeValue(event, message);
94+
}
8795
if ('code' in domException) {
8896
event.tags = { ...event.tags, 'DOMException.code': `${domException.code}` };
8997
}

0 commit comments

Comments
 (0)