-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(sveltekit): Add server-side handleError
wrapper
#7411
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 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
export * from '@sentry/svelte'; | ||
|
||
export { init } from './sdk'; | ||
export { wrapHandleError } from './handleError'; | ||
export { handleErrorWithSentry } 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
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,30 @@ | ||
import { captureException } from '@sentry/node'; | ||
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 { HandleServerError, RequestEvent } from '@sveltejs/kit'; | ||
|
||
/** | ||
* Wrapper for the SvelteKit error handler that sends the error to Sentry. | ||
* | ||
* @param handleError The original SvelteKit error handler. | ||
*/ | ||
export function handleErrorWithSentry(handleError?: HandleServerError): HandleServerError { | ||
return (input: { error: unknown; event: RequestEvent }): ReturnType<HandleServerError> => { | ||
captureException(input.error, scope => { | ||
scope.addEventProcessor(event => { | ||
addExceptionMechanism(event, { | ||
type: 'sveltekit', | ||
handled: false, | ||
}); | ||
return event; | ||
}); | ||
return scope; | ||
}); | ||
if (handleError) { | ||
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,4 @@ | ||
export * from '@sentry/node'; | ||
|
||
export { init } from './sdk'; | ||
export { handleErrorWithSentry } from './handleError'; |
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,84 @@ | ||
import { Scope } from '@sentry/node'; | ||
// 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 { HandleServerError, RequestEvent } from '@sveltejs/kit'; | ||
|
||
import { wrapHandleErrorWithSentry } from '../../src/server/handleError'; | ||
|
||
const mockCaptureException = jest.fn(); | ||
let mockScope = new Scope(); | ||
|
||
jest.mock('@sentry/node', () => { | ||
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: RequestEvent }): ReturnType<HandleServerError> { | ||
return { | ||
message: 'Whoops!', | ||
}; | ||
} | ||
|
||
const requestEvent = {} as RequestEvent; | ||
|
||
describe('handleError', () => { | ||
beforeEach(() => { | ||
mockCaptureException.mockClear(); | ||
mockAddExceptionMechanism.mockClear(); | ||
mockScope = new Scope(); | ||
}); | ||
|
||
it('works when a handleError func is not provided', async () => { | ||
const wrappedHandleError = wrapHandleErrorWithSentry(); | ||
const mockError = new Error('test'); | ||
const returnVal = await wrappedHandleError({ error: mockError, event: requestEvent }); | ||
|
||
expect(returnVal).not.toBeDefined(); | ||
expect(mockCaptureException).toHaveBeenCalledTimes(1); | ||
expect(mockCaptureException).toHaveBeenCalledWith(mockError, expect.any(Function)); | ||
}); | ||
|
||
it('calls captureException', async () => { | ||
const wrappedHandleError = wrapHandleErrorWithSentry(handleError); | ||
const mockError = new Error('test'); | ||
const returnVal = await wrappedHandleError({ error: mockError, event: requestEvent }); | ||
|
||
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 = wrapHandleErrorWithSentry(handleError); | ||
const mockError = new Error('test'); | ||
await wrappedHandleError({ error: mockError, event: requestEvent }); | ||
|
||
expect(addEventProcessorSpy).toBeCalledTimes(1); | ||
expect(mockAddExceptionMechanism).toBeCalledTimes(1); | ||
expect(mockAddExceptionMechanism).toBeCalledWith({}, { handled: false, type: 'sveltekit' }); | ||
}); | ||
}); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
l: this needs to be updated I guess 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes! good catch.