-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RUM] Catch errors thrown by user callbacks (#745)
* Add catchErrors function * Use catchErrors to wrap beforeSend * limitModification does not need to catch errors anymore * Use catchErrors to wrap onNewLocation * Use catchErrors to wrap onReady callbacks before initilization * Improve error message Co-authored-by: Benoît <[email protected]> * 👌 Move catchErrors to buildConfiguration * 👌 Rename to catchUserErrors * 👌 Refactor the code * 👌 Test catching errors on onReady callbacks before init * Fix typo * Fix tests * Refactor code Co-authored-by: Bastien Caudan <[email protected]> * Move onNewLocation wrapping to startRumEventCollection This is needed to make the test able to pass its own Lifecycle object and test that onNewLocation catches user errors after being wrapped. The stopViewCollection is needed because startViewCollection attaches a listener to history changes, and if not removed, it will be executed on all following tests and generates unwanted console.errors * 👌 Test catching errors of beforeSend and onNewLocation callbacks Co-authored-by: Benoît <[email protected]> Co-authored-by: Bastien Caudan <[email protected]>
- Loading branch information
1 parent
2662812
commit a12910e
Showing
13 changed files
with
161 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { defineGlobal } from './init' | ||
|
||
describe('defineGlobal', () => { | ||
it('adds new property to the global object', () => { | ||
const myGlobal = {} as any | ||
const value = 'my value' | ||
defineGlobal(myGlobal, 'foo', value) | ||
expect(myGlobal.foo).toBe(value) | ||
}) | ||
|
||
it('overrides property if exists on the global object', () => { | ||
const myGlobal = { foo: 'old value' } | ||
const value = 'my value' | ||
defineGlobal(myGlobal, 'foo', value) | ||
expect(myGlobal.foo).toBe(value) | ||
}) | ||
|
||
it('run the queued callbacks on the old value', () => { | ||
const fn1 = jasmine.createSpy() | ||
const fn2 = jasmine.createSpy() | ||
const myGlobal: any = { | ||
foo: { | ||
q: [fn1, fn2], | ||
}, | ||
} | ||
const value = 'my value' | ||
defineGlobal(myGlobal, 'foo', value) | ||
expect(myGlobal.foo).toBe(value) | ||
expect(fn1).toHaveBeenCalled() | ||
expect(fn2).toHaveBeenCalled() | ||
}) | ||
|
||
it('catches the errors thrown by the queued callbacks', () => { | ||
const myError = 'Ooops!' | ||
const onReady = () => { | ||
throw myError | ||
} | ||
const myGlobal: any = { | ||
foo: { | ||
q: [onReady], | ||
}, | ||
} | ||
const consoleErrorSpy = spyOn(console, 'error') | ||
|
||
defineGlobal(myGlobal, 'foo', {}) | ||
expect(consoleErrorSpy).toHaveBeenCalledWith('onReady callback threw an error:', myError) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { catchUserErrors } from './catchUserErrors' | ||
|
||
describe('catchUserErrors', () => { | ||
it('returns the same result as the original function', () => { | ||
const wrappedFn = catchUserErrors((a: number, b: number) => a + b, 'Error during callback') | ||
expect(wrappedFn(10, 2)).toBe(12) | ||
}) | ||
|
||
it('logs errors using console.error and returns undefined', () => { | ||
const consoleErrorSpy = spyOn(console, 'error') | ||
const myError = 'Ooops!' | ||
const wrappedFn = catchUserErrors(() => { | ||
throw myError | ||
}, 'Error during callback') | ||
expect(wrappedFn()).toBe(undefined) | ||
expect(consoleErrorSpy).toHaveBeenCalledWith('Error during callback', myError) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export function catchUserErrors<Args extends any[], R>(fn: (...args: Args) => R, errorMsg: string) { | ||
return (...args: Args) => { | ||
try { | ||
return fn(...args) | ||
} catch (err) { | ||
console.error(errorMsg, err) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters