Skip to content

Commit

Permalink
feat: align browser reported uncaught syntax errors (#881)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhousley authored Feb 9, 2024
1 parent f60e7f1 commit d4a0f30
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/features/jserrors/instrument/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ export class Instrument extends InstrumentBase {
* @returns {Error|UncaughtError} The error event converted to an Error object
*/
#castErrorEvent (errorEvent) {
if (errorEvent.error instanceof SyntaxError && !/:\d+$/.test(errorEvent.error.stack?.trim())) {
const error = new UncaughtError(errorEvent.message, errorEvent.filename, errorEvent.lineno, errorEvent.colno)
error.name = SyntaxError.name
return error
}

if (errorEvent.error instanceof Error) {
return errorEvent.error
}
Expand Down
24 changes: 24 additions & 0 deletions tests/assets/js-error-syntax-error.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<!--
Copyright 2020 New Relic Corporation.
PDX-License-Identifier: Apache-2.0
-->
<html>
<head>
<title>RUM Unit Test</title>
{init} {config} {loader}
</head>
<body>
This page throws an uncaught SyntaxError exception

<!-- Fail with syntax error -->
<script>
-
</script>

<!-- Fail with an eval syntax error -->
<script>
eval('-')
</script>
</body>
</html>
44 changes: 44 additions & 0 deletions tests/specs/err/index.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,48 @@ describe('basic error capturing', () => {
expect(errors.request.body.err.length).toEqual(expected.length)
expect(errors.request.body.err).toEqual(expect.arrayContaining(expected))
})

it('should capture file and line number for syntax errors', async () => {
const [errors] = await Promise.all([
browser.testHandle.expectErrors(),
browser.url(await browser.testHandle.assetURL('js-error-syntax-error.html'))
.then(() => browser.waitForAgentLoad())
])

if (browserMatch(notIE)) {
expect(errors.request.body.err).toEqual(expect.arrayContaining([
expect.objectContaining({
params: expect.objectContaining({
stackHash: 334471736,
exceptionClass: 'SyntaxError',
stack_trace: expect.stringContaining('<inline>:18')
})
}),
expect.objectContaining({
params: expect.objectContaining({
stackHash: 334471761,
exceptionClass: 'SyntaxError',
stack_trace: expect.stringContaining('<inline>:22')
})
})
]))
} else {
expect(errors.request.body.err).toEqual(expect.arrayContaining([
expect.objectContaining({
params: expect.objectContaining({
stackHash: 334471735,
exceptionClass: 'UncaughtError',
stack_trace: expect.stringContaining('<inline>:17')
})
}),
expect.objectContaining({
params: expect.objectContaining({
stackHash: 334471761,
exceptionClass: 'unknown',
stack_trace: expect.stringContaining('<inline>:22')
})
})
]))
}
})
})

0 comments on commit d4a0f30

Please sign in to comment.