Skip to content

Commit f16b753

Browse files
committed
fix(browser): Add a synthetic stack trace to DOMException with empty stack traces if attachStacktrace is true
1 parent a1df56d commit f16b753

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

packages/browser/src/eventbuilder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ export function eventFromUnknownInput(
288288
if (isDOMError(exception) || isDOMException(exception as DOMException)) {
289289
const domException = exception as DOMException;
290290

291-
if ('stack' in (exception as Error)) {
291+
if ((exception as Error).stack) {
292292
event = eventFromError(stackParser, exception as Error);
293293
} else {
294294
const name = domException.name || (isDOMError(domException) ? 'DOMError' : 'DOMException');

packages/browser/test/eventbuilder.test.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,81 @@ describe('eventFromUnknownInput', () => {
168168
},
169169
});
170170
});
171+
172+
it('add a synthetic stack trace to DOMException with empty stack traces if attachStacktrace is true', async () => {
173+
const exception = new DOMException('The string did not match the expected pattern.', 'SyntaxError');
174+
exception.stack = '';
175+
176+
const syntheticException = new Error('Test message');
177+
const event = await eventFromUnknownInput(defaultStackParser, exception, syntheticException, true);
178+
expect(event.exception?.values?.[0]).toEqual(
179+
expect.objectContaining({
180+
mechanism: { handled: true, synthetic: true, type: 'generic' },
181+
stacktrace: {
182+
frames: expect.arrayContaining([expect.any(Object), expect.any(Object)]),
183+
},
184+
type: 'Error',
185+
value: 'SyntaxError: The string did not match the expected pattern.',
186+
}),
187+
);
188+
});
189+
190+
it('add a synthetic stack trace to DOMException without a stack traces property if attachStacktrace is true', async () => {
191+
const exception = new DOMException('The string did not match the expected pattern.', 'SyntaxError');
192+
delete exception.stack;
193+
194+
const syntheticException = new Error('Test message');
195+
const event = await eventFromUnknownInput(defaultStackParser, exception, syntheticException, true);
196+
expect(event.exception?.values?.[0]).toEqual(
197+
expect.objectContaining({
198+
mechanism: { handled: true, synthetic: true, type: 'generic' },
199+
stacktrace: {
200+
frames: expect.arrayContaining([expect.any(Object), expect.any(Object)]),
201+
},
202+
type: 'Error',
203+
value: 'SyntaxError: The string did not match the expected pattern.',
204+
}),
205+
);
206+
});
207+
208+
it("doesn't add a synthetic stack trace to DOMException with empty stack traces if attachStacktrace is false", async () => {
209+
const exception = new DOMException('The string did not match the expected pattern.', 'SyntaxError');
210+
exception.stack = '';
211+
212+
const syntheticException = new Error('Test message');
213+
const event = await eventFromUnknownInput(defaultStackParser, exception, syntheticException, false);
214+
expect(event.exception?.values?.[0]).toEqual(
215+
{
216+
type: 'Error',
217+
value: 'SyntaxError: The string did not match the expected pattern.',
218+
},
219+
);
220+
});
221+
222+
it("doesn't add a synthetic stack trace to DOMException with stack traces if attachStacktrace is true", async () => {
223+
const exception = new DOMException('The string did not match the expected pattern.', 'SyntaxError');
224+
exception.stack = 'SyntaxError\n at <anonymous>:1:2';
225+
226+
const syntheticException = new Error('Test message');
227+
const event = await eventFromUnknownInput(defaultStackParser, exception, syntheticException, true);
228+
expect(event.exception?.values?.[0]).toEqual(
229+
expect.objectContaining({
230+
stacktrace: {
231+
frames: [
232+
{
233+
colno: 2,
234+
filename: "<anonymous>",
235+
function: "?",
236+
in_app: true,
237+
lineno: 1,
238+
},
239+
],
240+
},
241+
type: 'SyntaxError',
242+
value: 'The string did not match the expected pattern.',
243+
}),
244+
);
245+
});
171246
});
172247

173248
describe('extractMessage', () => {

0 commit comments

Comments
 (0)