-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make error handler and logger mandatory in all components (#2007)
* feat: improve user callback execution * feat: make error handler and logger mandatory in all components
- Loading branch information
1 parent
bc050e6
commit 63c7f23
Showing
55 changed files
with
1,026 additions
and
754 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
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
79 changes: 72 additions & 7 deletions
79
packages/analytics-js-common/__tests__/utilities/errors.test.ts
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 |
---|---|---|
@@ -1,18 +1,83 @@ | ||
import { dispatchErrorEvent } from '../../src/utilities/errors'; | ||
import { dispatchErrorEvent, getStacktrace } from '../../src/utilities/errors'; | ||
|
||
describe('Errors - utilities', () => { | ||
describe('dispatchErrorEvent', () => { | ||
const dispatchEventMock = jest.fn(); | ||
const originalDispatchEvent = globalThis.dispatchEvent; | ||
|
||
beforeEach(() => { | ||
globalThis.dispatchEvent = dispatchEventMock; | ||
}); | ||
|
||
afterEach(() => { | ||
globalThis.dispatchEvent = originalDispatchEvent; | ||
}); | ||
|
||
it('should dispatch an error event', () => { | ||
const dispatchEvent = jest.fn(); | ||
const originalDispatchEvent = globalThis.dispatchEvent; | ||
const error = new Error('Test error'); | ||
|
||
dispatchErrorEvent(error); | ||
|
||
globalThis.dispatchEvent = dispatchEvent; | ||
expect(dispatchEventMock).toHaveBeenCalledWith(new ErrorEvent('error', { error })); | ||
expect((error.stack as string).endsWith('[SDK DISPATCHED ERROR]')).toBeTruthy(); | ||
}); | ||
|
||
it('should decorate stacktrace before dispatching error event', () => { | ||
const error = new Error('Test error'); | ||
// @ts-expect-error need to set the value for testing | ||
error.stacktrace = error.stack; | ||
delete error.stack; | ||
|
||
dispatchErrorEvent(error); | ||
expect(dispatchEvent).toHaveBeenCalledWith(new ErrorEvent('error', { error })); | ||
|
||
// Cleanup | ||
globalThis.dispatchEvent = originalDispatchEvent; | ||
// @ts-expect-error need to check the stacktrace property | ||
expect((error.stacktrace as string).endsWith('[SDK DISPATCHED ERROR]')).toBeTruthy(); | ||
}); | ||
|
||
it('should decorate opera sourceloc before dispatching error event', () => { | ||
const error = new Error('Test error'); | ||
// @ts-expect-error need to set the value for testing | ||
error['opera#sourceloc'] = error.stack; | ||
delete error.stack; | ||
|
||
dispatchErrorEvent(error); | ||
|
||
// @ts-expect-error need to check the opera sourceloc property | ||
expect((error['opera#sourceloc'] as string).endsWith('[SDK DISPATCHED ERROR]')).toBeTruthy(); | ||
}); | ||
}); | ||
|
||
describe('getStacktrace', () => { | ||
it('should return stack if it is a string', () => { | ||
const error = new Error('Test error'); | ||
expect(getStacktrace(error)).toBe(error.stack); | ||
}); | ||
|
||
it('should return stacktrace if it is a string', () => { | ||
const error = new Error('Test error'); | ||
// @ts-expect-error need to set the value for testing | ||
error.stacktrace = error.stack; | ||
delete error.stack; | ||
|
||
// @ts-expect-error need to check the stacktrace property | ||
expect(getStacktrace(error)).toBe(error.stacktrace); | ||
}); | ||
|
||
it('should return opera sourceloc if it is a string', () => { | ||
const error = new Error('Test error'); | ||
// @ts-expect-error need to set the value for testing | ||
error['opera#sourceloc'] = error.stack; | ||
delete error.stack; | ||
|
||
// @ts-expect-error need to check the opera sourceloc property | ||
expect(getStacktrace(error)).toBe(error['opera#sourceloc']); | ||
}); | ||
|
||
it('should return undefined if none of the properties are strings', () => { | ||
const error = new Error('Test error'); | ||
delete error.stack; | ||
|
||
expect(getStacktrace(error)).toBeUndefined(); | ||
}); | ||
}); | ||
}); |
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
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
Oops, something went wrong.