Skip to content

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 3 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
rename to handleErrorWithSentry
  • Loading branch information
AbhiPrasad committed Mar 10, 2023
commit e9d89040602744cf0add176a47e47f42e82b746d
6 changes: 4 additions & 2 deletions packages/sveltekit/src/client/handleError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { HandleClientError, NavigationEvent } from '@sveltejs/kit';
*
* @param handleError The original SvelteKit error handler.
*/
export function wrapHandleErrorWithSentry(handleError: HandleClientError): HandleClientError {
export function handleErrorWithSentry(handleError?: HandleClientError): HandleClientError {
return (input: { error: unknown; event: NavigationEvent }): ReturnType<HandleClientError> => {
captureException(input.error, scope => {
scope.addEventProcessor(event => {
Expand All @@ -23,6 +23,8 @@ export function wrapHandleErrorWithSentry(handleError: HandleClientError): Handl
});
return scope;
});
return handleError(input);
if (handleError) {
return handleError(input);
}
};
}
2 changes: 1 addition & 1 deletion packages/sveltekit/src/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export * from '@sentry/svelte';

export { init } from './sdk';
export { wrapHandleErrorWithSentry } from './handleError';
export { handleErrorWithSentry } from './handleError';

// Just here so that eslint is happy until we export more stuff here
export const PLACEHOLDER_CLIENT = 'PLACEHOLDER';
2 changes: 1 addition & 1 deletion packages/sveltekit/src/index.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import type * as serverSdk from './server';
/** Initializes Sentry SvelteKit SDK */
export declare function init(options: Options | clientSdk.BrowserOptions | serverSdk.NodeOptions): void;

export declare function wrapHandleErrorWithSentry<T extends HandleClientError | HandleServerError>(
export declare function handleErrorWithSentry<T extends HandleClientError | HandleServerError>(
handleError: T,
): ReturnType<T>;

Expand Down
6 changes: 4 additions & 2 deletions packages/sveltekit/src/server/handleError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { HandleServerError, RequestEvent } from '@sveltejs/kit';
*
* @param handleError The original SvelteKit error handler.
*/
export function wrapHandleErrorWithSentry(handleError: HandleServerError): HandleServerError {
export function handleErrorWithSentry(handleError?: HandleServerError): HandleServerError {
return (input: { error: unknown; event: RequestEvent }): ReturnType<HandleServerError> => {
captureException(input.error, scope => {
scope.addEventProcessor(event => {
Expand All @@ -23,6 +23,8 @@ export function wrapHandleErrorWithSentry(handleError: HandleServerError): Handl
});
return scope;
});
return handleError(input);
if (handleError) {
return handleError(input);
}
};
}
2 changes: 1 addition & 1 deletion packages/sveltekit/src/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from '@sentry/node';

export { init } from './sdk';
export { wrapHandleErrorWithSentry } from './handleError';
export { handleErrorWithSentry } from './handleError';
10 changes: 10 additions & 0 deletions packages/sveltekit/test/client/handleError.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ describe('handleError', () => {
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: navigationEvent });

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');
Expand Down
10 changes: 10 additions & 0 deletions packages/sveltekit/test/server/handleError.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ describe('handleError', () => {
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');
Expand Down