-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(sveltekit): Introduce client-side handleError
wrapper
#7406
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
11b336a
feat(sveltekit): Introduce client-side `handleError` wrapper
AbhiPrasad e5e180f
Update package.json exports field
AbhiPrasad f983b2c
lint fix
AbhiPrasad c99d29a
clean up conditional exports
AbhiPrasad 2a9e94b
server
AbhiPrasad 053c954
lint fix
AbhiPrasad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,28 @@ | ||
import { captureException } from '@sentry/svelte'; | ||
import { addExceptionMechanism } from '@sentry/utils'; | ||
// For now disable the import/no-unresolved rule, because we don't have a way to | ||
// tell eslint that we are only importing types from the @sveltejs/kit package without | ||
// adding a custom resolver, which will take too much time. | ||
// eslint-disable-next-line import/no-unresolved | ||
import type { HandleClientError, NavigationEvent } from '@sveltejs/kit'; | ||
|
||
/** | ||
* Wrapper for the SvelteKit error handler that sends the error to Sentry. | ||
* | ||
* @param handleError The original SvelteKit error handler. | ||
*/ | ||
export function wrapHandleError(handleError: HandleClientError): HandleClientError { | ||
return (input: { error: unknown; event: NavigationEvent }): ReturnType<HandleClientError> => { | ||
Lms24 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
captureException(input.error, scope => { | ||
scope.addEventProcessor(event => { | ||
addExceptionMechanism(event, { | ||
type: 'sveltekit', | ||
handled: false, | ||
}); | ||
return event; | ||
}); | ||
return scope; | ||
}); | ||
return handleError(input); | ||
}; | ||
} |
This file contains hidden or 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,3 +1,7 @@ | ||
export * from '@sentry/svelte'; | ||
|
||
export { init } from './sdk'; | ||
export { wrapHandleError } from './handleError'; | ||
|
||
// Just here so that eslint is happy until we export more stuff here | ||
export const PLACEHOLDER_CLIENT = 'PLACEHOLDER'; |
This file contains hidden or 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,83 @@ | ||
import { Scope } from '@sentry/svelte'; | ||
|
||
// For now disable the import/no-unresolved rule, because we don't have a way to | ||
// tell eslint that we are only importing types from the @sveltejs/kit package without | ||
// adding a custom resolver, which will take too much time. | ||
// eslint-disable-next-line import/no-unresolved | ||
import type { HandleClientError, NavigationEvent } from '@sveltejs/kit'; | ||
|
||
import { wrapHandleError } from '../../src/client/handleError'; | ||
|
||
const mockCaptureException = jest.fn(); | ||
let mockScope = new Scope(); | ||
|
||
jest.mock('@sentry/svelte', () => { | ||
const original = jest.requireActual('@sentry/core'); | ||
return { | ||
...original, | ||
captureException: (err: unknown, cb: (arg0: unknown) => unknown) => { | ||
cb(mockScope); | ||
mockCaptureException(err, cb); | ||
return original.captureException(err, cb); | ||
}, | ||
}; | ||
}); | ||
|
||
const mockAddExceptionMechanism = jest.fn(); | ||
|
||
jest.mock('@sentry/utils', () => { | ||
const original = jest.requireActual('@sentry/utils'); | ||
return { | ||
...original, | ||
addExceptionMechanism: (...args: unknown[]) => mockAddExceptionMechanism(...args), | ||
}; | ||
}); | ||
|
||
function handleError(_input: { error: unknown; event: NavigationEvent }): ReturnType<HandleClientError> { | ||
return { | ||
message: 'Whoops!', | ||
}; | ||
} | ||
|
||
const navigationEvent: NavigationEvent = { | ||
params: { | ||
id: '123', | ||
}, | ||
route: { | ||
id: 'users/[id]', | ||
}, | ||
url: new URL('http://example.org/users/123'), | ||
}; | ||
|
||
describe('handleError', () => { | ||
beforeEach(() => { | ||
mockCaptureException.mockClear(); | ||
mockAddExceptionMechanism.mockClear(); | ||
mockScope = new Scope(); | ||
}); | ||
|
||
it('calls captureException', async () => { | ||
const wrappedHandleError = wrapHandleError(handleError); | ||
const mockError = new Error('test'); | ||
const returnVal = await wrappedHandleError({ error: mockError, event: navigationEvent }); | ||
|
||
expect(returnVal!.message).toEqual('Whoops!'); | ||
expect(mockCaptureException).toHaveBeenCalledTimes(1); | ||
expect(mockCaptureException).toHaveBeenCalledWith(mockError, expect.any(Function)); | ||
}); | ||
|
||
it('adds an exception mechanism', async () => { | ||
const addEventProcessorSpy = jest.spyOn(mockScope, 'addEventProcessor').mockImplementationOnce(callback => { | ||
void callback({}, { event_id: 'fake-event-id' }); | ||
return mockScope; | ||
}); | ||
|
||
const wrappedHandleError = wrapHandleError(handleError); | ||
const mockError = new Error('test'); | ||
await wrappedHandleError({ error: mockError, event: navigationEvent }); | ||
|
||
expect(addEventProcessorSpy).toBeCalledTimes(1); | ||
expect(mockAddExceptionMechanism).toBeCalledTimes(1); | ||
expect(mockAddExceptionMechanism).toBeCalledWith({}, { handled: false, type: 'sveltekit' }); | ||
}); | ||
}); |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.