Skip to content

Commit e52847e

Browse files
authored
fix(sveltekit): Avoid double-wrapping load functions (#8094)
Applying the `wrap(server)?LoadWithSentry` wrappers multiple times shouldn't lead to double wrapping but instead we should detect if we already wrapped a load function and no-op in this case. This patch adds a flag to the respective load events to detect double wrapping.
1 parent 6bf5d3a commit e52847e

File tree

5 files changed

+153
-76
lines changed

5 files changed

+153
-76
lines changed

packages/sveltekit/src/client/load.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { captureException } from '@sentry/svelte';
66
import type { ClientOptions, SanitizedRequestData } from '@sentry/types';
77
import {
88
addExceptionMechanism,
9+
addNonEnumerableProperty,
910
getSanitizedUrlString,
1011
objectify,
1112
parseFetchArgs,
@@ -14,8 +15,11 @@ import {
1415
} from '@sentry/utils';
1516
import type { LoadEvent } from '@sveltejs/kit';
1617

18+
import type { SentryWrappedFlag } from '../common/utils';
1719
import { isRedirect } from '../common/utils';
1820

21+
type PatchedLoadEvent = LoadEvent & Partial<SentryWrappedFlag>;
22+
1923
function sendErrorToSentry(e: unknown): unknown {
2024
// In case we have a primitive, wrap it in the equivalent wrapper class (string -> String, etc.) so that we can
2125
// store a seen flag on it.
@@ -66,13 +70,20 @@ export function wrapLoadWithSentry<T extends (...args: any) => any>(origLoad: T)
6670
return new Proxy(origLoad, {
6771
apply: (wrappingTarget, thisArg, args: Parameters<T>) => {
6872
// Type casting here because `T` cannot extend `Load` (see comment above function signature)
69-
const event = args[0] as LoadEvent;
73+
const event = args[0] as PatchedLoadEvent;
7074

71-
const patchedEvent = {
75+
// Check if already wrapped
76+
if (event.__sentry_wrapped__) {
77+
return wrappingTarget.apply(thisArg, args);
78+
}
79+
80+
const patchedEvent: PatchedLoadEvent = {
7281
...event,
7382
fetch: instrumentSvelteKitFetch(event.fetch),
7483
};
7584

85+
addNonEnumerableProperty(patchedEvent as unknown as Record<string, unknown>, '__sentry_wrapped__', true);
86+
7687
const routeId = event.route.id;
7788
return trace(
7889
{

packages/sveltekit/src/common/utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import type { Redirect } from '@sveltejs/kit';
22

3+
export type SentryWrappedFlag = {
4+
/**
5+
* If this flag is set, we know that the load event was already wrapped once
6+
* and we shouldn't wrap it again.
7+
*/
8+
__sentry_wrapped__?: true;
9+
};
10+
311
/**
412
* Determines if a thrown "error" is a Redirect object which SvelteKit users can throw to redirect to another route
513
* see: https://kit.svelte.dev/docs/modules#sveltejs-kit-redirect

packages/sveltekit/src/server/load.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22
import { trace } from '@sentry/core';
33
import { captureException } from '@sentry/node';
44
import type { TransactionContext } from '@sentry/types';
5-
import { addExceptionMechanism, objectify } from '@sentry/utils';
5+
import { addExceptionMechanism, addNonEnumerableProperty, objectify } from '@sentry/utils';
66
import type { HttpError, LoadEvent, ServerLoadEvent } from '@sveltejs/kit';
77

8+
import type { SentryWrappedFlag } from '../common/utils';
89
import { isRedirect } from '../common/utils';
910
import { getTracePropagationData } from './utils';
1011

12+
type PatchedLoadEvent = LoadEvent & SentryWrappedFlag;
13+
type PatchedServerLoadEvent = ServerLoadEvent & SentryWrappedFlag;
14+
1115
function isHttpError(err: unknown): err is HttpError {
1216
return typeof err === 'object' && err !== null && 'status' in err && 'body' in err;
1317
}
@@ -59,7 +63,15 @@ export function wrapLoadWithSentry<T extends (...args: any) => any>(origLoad: T)
5963
return new Proxy(origLoad, {
6064
apply: (wrappingTarget, thisArg, args: Parameters<T>) => {
6165
// Type casting here because `T` cannot extend `Load` (see comment above function signature)
62-
const event = args[0] as LoadEvent;
66+
// Also, this event possibly already has a sentry wrapped flag attached
67+
const event = args[0] as PatchedLoadEvent;
68+
69+
if (event.__sentry_wrapped__) {
70+
return wrappingTarget.apply(thisArg, args);
71+
}
72+
73+
addNonEnumerableProperty(event as unknown as Record<string, unknown>, '__sentry_wrapped__', true);
74+
6375
const routeId = event.route && event.route.id;
6476

6577
const traceLoadContext: TransactionContext = {
@@ -102,7 +114,15 @@ export function wrapServerLoadWithSentry<T extends (...args: any) => any>(origSe
102114
return new Proxy(origServerLoad, {
103115
apply: (wrappingTarget, thisArg, args: Parameters<T>) => {
104116
// Type casting here because `T` cannot extend `ServerLoad` (see comment above function signature)
105-
const event = args[0] as ServerLoadEvent;
117+
// Also, this event possibly already has a sentry wrapped flag attached
118+
const event = args[0] as PatchedServerLoadEvent;
119+
120+
if (event.__sentry_wrapped__) {
121+
return wrappingTarget.apply(thisArg, args);
122+
}
123+
124+
addNonEnumerableProperty(event as unknown as Record<string, unknown>, '__sentry_wrapped__', true);
125+
106126
const routeId = event.route && event.route.id;
107127

108128
const { dynamicSamplingContext, traceparentData } = getTracePropagationData(event);

packages/sveltekit/test/client/load.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,4 +450,17 @@ describe('wrapLoadWithSentry', () => {
450450
{ handled: false, type: 'sveltekit', data: { function: 'load' } },
451451
);
452452
});
453+
454+
it("doesn't wrap load more than once if the wrapper was applied multiple times", async () => {
455+
async function load({ params }: Parameters<Load>[0]): Promise<ReturnType<Load>> {
456+
return {
457+
post: params.id,
458+
};
459+
}
460+
461+
const wrappedLoad = wrapLoadWithSentry(wrapLoadWithSentry(load));
462+
await wrappedLoad(MOCK_LOAD_ARGS);
463+
464+
expect(mockTrace).toHaveBeenCalledTimes(1);
465+
});
453466
});

0 commit comments

Comments
 (0)