Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
3324a90
Merge pull request #9853 from getsentry/master
github-actions[bot] Dec 14, 2023
383c816
build: Enable codecov AI PR review (#9850)
AbhiPrasad Dec 14, 2023
1e8d2b3
ref: Use `getCurrentScope()`/`hub.getScope()` instead of `configureSc…
mydea Dec 15, 2023
da8c9c3
feat(nextjs): Auto instrument generation functions (#9781)
Dec 15, 2023
30bb8b5
fix(utils): Do not use `Event` type in worldwide (#9864)
mydea Dec 15, 2023
ffc4181
ref: Use `getCurrentScope()` / `getClient` instead of `hub.xxx` (#9862)
mydea Dec 15, 2023
5bc9a38
fix(utils): Update `eventFromUnknownInput` to avoid scope pollution &…
mydea Dec 15, 2023
72c3488
ref(node): Refactor node integrations to use `processEvent` (#9018)
mydea Dec 15, 2023
e128936
fix(utils): Support crypto.getRandomValues in old Chromium versions (…
jghinestrosa Dec 15, 2023
53724c5
meta(feedback): Fix syntax error in README (#9875)
billyvg Dec 15, 2023
12c146b
fix(nextjs): Export `createReduxEnhancer` (#9854)
adam187 Dec 18, 2023
db4bef1
ref: Pass client instead of hub to `isSentryRequestUrl` (#9869)
mydea Dec 18, 2023
f2a4caa
feat(core): Update `withScope` to return callback return value (#9866)
mydea Dec 18, 2023
01a4cc9
feat(core): Add type & utility for function-based integrations (#9818)
mydea Dec 18, 2023
c0c5eca
feat(node): Add Hapi Integration (#9539)
onurtemizkan Dec 18, 2023
35906d0
ref: Use `addBreadcrumb` directly & allow to pass hint (#9867)
mydea Dec 18, 2023
f922414
build: Fix linting (#9888)
mydea Dec 18, 2023
e1d3633
ref(serverless): Avoid using `pushScope` (#9883)
mydea Dec 18, 2023
ada32b2
ref(nextjs): Simplify `wrapServerComponentWithSentry` (#9844)
Dec 18, 2023
0efdb21
feat(core): Deprecate `configureScope` (#9887)
mydea Dec 18, 2023
b27c236
feat(core): Deprecate `pushScope` & `popScope` (#9890)
mydea Dec 18, 2023
f121585
feat(nextjs): Connect server component transactions if there is no in…
Dec 18, 2023
8fb1a2f
feat(deno): Support `Deno.CronSchedule` for cron jobs (#9880)
timfish Dec 18, 2023
84299d0
feat(replay): Add `canvas.type` setting (#9877)
billyvg Dec 18, 2023
605fd50
feat(node-experimental): Update to new Scope APIs (#9799)
mydea Dec 19, 2023
32f4bf0
ref(deno): Refactor deno integration to avoid `setupOnce` (#9900)
mydea Dec 19, 2023
91a6b4e
ref(browser): Refactor browser integrations to avoid `setupOnce` (#9898)
mydea Dec 19, 2023
c9552a4
ref(node): Refactor LocalVariables integration to avoid `setupOnce` (…
mydea Dec 19, 2023
4e0c460
chore(sveltekit): Add SvelteKit 2.0 to peer dependencies (#9861)
Lms24 Dec 19, 2023
6d32228
fix(sveltekit): Add conditional exports (#9872)
Lms24 Dec 19, 2023
a856913
fix(remix): Do not capture thrown redirect responses. (#9909)
onurtemizkan Dec 19, 2023
31c769c
test(sveltekit): Add SvelteKit 2.0 E2E test app (#9873)
Lms24 Dec 19, 2023
cef3621
ref(sveltekit): Improve SvelteKit 2.0 404 server error handling (#9901)
Lms24 Dec 19, 2023
cf773fc
fix(sveltekit): Avoid capturing 404 errors on client side (#9902)
Lms24 Dec 19, 2023
9f173e1
meta(changelog): Update changelog for 7.89.0
Dec 19, 2023
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
ref(sveltekit): Improve SvelteKit 2.0 404 server error handling (#9901)
Improve our logic to filter out "Not Found" errors in our
server-side `handleError` hook wrapper:

- We now use SvelteKit 2.0 - native [error
properties](https://kit.svelte.dev/docs/migrating-to-sveltekit-2#improved-error-handling)
(`status` ~and `message`~) to check for "Not Found" errors
- Adjusted types for type safety and backwards compatibility with
SvelteKit 1.x where the ~two properties~ property don't exist.
- Updated Sveltekit to 2.0 in our dev dependency to work with latest
types.
  • Loading branch information
Lms24 authored Dec 19, 2023
commit cef3621ce44067992a9ab530d81ef6966bf2b9d3
6 changes: 3 additions & 3 deletions packages/sveltekit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@
"sorcery": "0.11.0"
},
"devDependencies": {
"@sveltejs/kit": "^1.11.0",
"@sveltejs/kit": "^2.0.2",
"rollup": "^3.20.2",
"svelte": "^3.44.0",
"vite": "4.0.5"
"svelte": "^4.2.8",
"vite": "^5.0.10"
},
"scripts": {
"build": "run-p build:transpile build:types",
Expand Down
5 changes: 4 additions & 1 deletion packages/sveltekit/src/client/handleError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ function defaultErrorHandler({ error }: Parameters<HandleClientError>[0]): Retur
});
}

// TODO: add backwards-compatible type for kit 1.x (soon)
type HandleClientErrorInput = Parameters<HandleClientError>[0];

/**
* Wrapper for the SvelteKit error handler that sends the error to Sentry.
*
* @param handleError The original SvelteKit error handler.
*/
export function handleErrorWithSentry(handleError: HandleClientError = defaultErrorHandler): HandleClientError {
return (input: { error: unknown; event: NavigationEvent }): ReturnType<HandleClientError> => {
return (input: HandleClientErrorInput): ReturnType<HandleClientError> => {
captureException(input.error, {
mechanism: {
type: 'sveltekit',
Expand Down
35 changes: 28 additions & 7 deletions packages/sveltekit/src/server/handleError.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { captureException } from '@sentry/node';
import type { HandleServerError, RequestEvent } from '@sveltejs/kit';
import type { HandleServerError } from '@sveltejs/kit';

import { flushIfServerless } from './utils';

Expand All @@ -11,14 +11,28 @@ function defaultErrorHandler({ error }: Parameters<HandleServerError>[0]): Retur
console.error(error && error.stack);
}

type HandleServerErrorInput = Parameters<HandleServerError>[0];

/**
* Backwards-compatible HandleServerError Input type for SvelteKit 1.x and 2.x
* `message` and `status` were added in 2.x.
* For backwards-compatibility, we make them optional
*
* @see https://kit.svelte.dev/docs/migrating-to-sveltekit-2#improved-error-handling
*/
type SafeHandleServerErrorInput = Omit<HandleServerErrorInput, 'status' | 'message'> &
Partial<Pick<HandleServerErrorInput, 'status' | 'message'>>;

/**
* Wrapper for the SvelteKit error handler that sends the error to Sentry.
*
* @param handleError The original SvelteKit error handler.
*/
export function handleErrorWithSentry(handleError: HandleServerError = defaultErrorHandler): HandleServerError {
return async (input: { error: unknown; event: RequestEvent }): Promise<void | App.Error> => {
return async (input: SafeHandleServerErrorInput): Promise<void | App.Error> => {
if (isNotFoundError(input)) {
// We're extra cautious with SafeHandleServerErrorInput - this type is not compatible with HandleServerErrorInput
// @ts-expect-error - we're still passing the same object, just with a different (backwards-compatible) type
return handleError(input);
}

Expand All @@ -31,19 +45,26 @@ export function handleErrorWithSentry(handleError: HandleServerError = defaultEr

await flushIfServerless();

// We're extra cautious with SafeHandleServerErrorInput - this type is not compatible with HandleServerErrorInput
// @ts-expect-error - we're still passing the same object, just with a different (backwards-compatible) type
return handleError(input);
};
}

/**
* When a page request fails because the page is not found, SvelteKit throws a "Not found" error.
* In the error handler here, we can't access the response yet (which we do in the load instrumentation),
* so we have to check if the error is a "Not found" error by checking if the route id is missing and
* by checking the error message on top of the raw stack trace.
*/
function isNotFoundError(input: { error: unknown; event: RequestEvent }): boolean {
const { error, event } = input;
function isNotFoundError(input: SafeHandleServerErrorInput): boolean {
const { error, event, status } = input;

// SvelteKit 2.0 offers a reliable way to check for a Not Found error:
if (status === 404) {
return true;
}

// SvelteKit 1.x doesn't offer a reliable way to check for a Not Found error.
// So we check the route id (shouldn't exist) and the raw stack trace
// We can delete all of this below whenever we drop Kit 1.x support
const hasNoRouteId = !event.route || !event.route.id;

const rawStack: string =
Expand Down
2 changes: 1 addition & 1 deletion packages/sveltekit/test/common/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('isHttpError', () => {
expect(isHttpError(httpErrorObject)).toBe(true);
});

it.each([new Error(), redirect(301, '/users/id'), 'string error', { status: 404 }, { body: 'Not found' }])(
it.each([new Error(), { status: 301, message: '/users/id' }, 'string error', { status: 404 }, { body: 'Not found' }])(
'returns `false` for other thrown objects (%s)',
httpErrorObject => {
expect(isHttpError(httpErrorObject)).toBe(false);
Expand Down
32 changes: 29 additions & 3 deletions packages/sveltekit/test/server/handleError.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('handleError', () => {
consoleErrorSpy.mockClear();
});

it('doesn\'t capture "Not found" errors for incorrect navigations', async () => {
it('doesn\'t capture "Not found" errors for incorrect navigations [Kit 1.x]', async () => {
const wrappedHandleError = handleErrorWithSentry();
const mockError = new Error('Not found: /asdf/123');
const mockEvent = {
Expand All @@ -35,18 +35,39 @@ describe('handleError', () => {
// ...
} as RequestEvent;

// @ts-expect-error - purposefully omitting status and message to cover SvelteKit 1.x compatibility
const returnVal = await wrappedHandleError({ error: mockError, event: mockEvent });

expect(returnVal).not.toBeDefined();
expect(mockCaptureException).toHaveBeenCalledTimes(0);
expect(consoleErrorSpy).toHaveBeenCalledTimes(1);
});

it('doesn\'t capture "Not found" errors for incorrect navigations [Kit 2.x]', async () => {
const wrappedHandleError = handleErrorWithSentry();

const returnVal = await wrappedHandleError({
error: new Error('404 /asdf/123'),
event: requestEvent,
status: 404,
message: 'Not Found',
});

expect(returnVal).not.toBeDefined();
expect(mockCaptureException).toHaveBeenCalledTimes(0);
expect(consoleErrorSpy).toHaveBeenCalledTimes(1);
});

describe('calls captureException', () => {
it('invokes the default handler if no handleError func is provided', async () => {
const wrappedHandleError = handleErrorWithSentry();
const mockError = new Error('test');
const returnVal = await wrappedHandleError({ error: mockError, event: requestEvent });
const returnVal = await wrappedHandleError({
error: mockError,
event: requestEvent,
status: 500,
message: 'Internal Error',
});

expect(returnVal).not.toBeDefined();
expect(mockCaptureException).toHaveBeenCalledTimes(1);
Expand All @@ -58,7 +79,12 @@ describe('handleError', () => {
it('invokes the user-provided error handler', async () => {
const wrappedHandleError = handleErrorWithSentry(handleError);
const mockError = new Error('test');
const returnVal = (await wrappedHandleError({ error: mockError, event: requestEvent })) as any;
const returnVal = (await wrappedHandleError({
error: mockError,
event: requestEvent,
status: 500,
message: 'Internal Error',
})) as any;

expect(returnVal.message).toEqual('Whoops!');
expect(mockCaptureException).toHaveBeenCalledTimes(1);
Expand Down
Loading