|
2 | 2 | import { trace } from '@sentry/core'; |
3 | 3 | import { captureException } from '@sentry/node'; |
4 | 4 | import type { TransactionContext } from '@sentry/types'; |
5 | | -import { addExceptionMechanism, objectify } from '@sentry/utils'; |
| 5 | +import { addExceptionMechanism, addNonEnumerableProperty, objectify } from '@sentry/utils'; |
6 | 6 | import type { HttpError, LoadEvent, ServerLoadEvent } from '@sveltejs/kit'; |
7 | 7 |
|
| 8 | +import type { SentryWrappedFlag } from '../common/utils'; |
8 | 9 | import { isRedirect } from '../common/utils'; |
9 | 10 | import { getTracePropagationData } from './utils'; |
10 | 11 |
|
| 12 | +type PatchedLoadEvent = LoadEvent & SentryWrappedFlag; |
| 13 | +type PatchedServerLoadEvent = ServerLoadEvent & SentryWrappedFlag; |
| 14 | + |
11 | 15 | function isHttpError(err: unknown): err is HttpError { |
12 | 16 | return typeof err === 'object' && err !== null && 'status' in err && 'body' in err; |
13 | 17 | } |
@@ -59,7 +63,15 @@ export function wrapLoadWithSentry<T extends (...args: any) => any>(origLoad: T) |
59 | 63 | return new Proxy(origLoad, { |
60 | 64 | apply: (wrappingTarget, thisArg, args: Parameters<T>) => { |
61 | 65 | // 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 | + |
63 | 75 | const routeId = event.route && event.route.id; |
64 | 76 |
|
65 | 77 | const traceLoadContext: TransactionContext = { |
@@ -102,7 +114,15 @@ export function wrapServerLoadWithSentry<T extends (...args: any) => any>(origSe |
102 | 114 | return new Proxy(origServerLoad, { |
103 | 115 | apply: (wrappingTarget, thisArg, args: Parameters<T>) => { |
104 | 116 | // 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 | + |
106 | 126 | const routeId = event.route && event.route.id; |
107 | 127 |
|
108 | 128 | const { dynamicSamplingContext, traceparentData } = getTracePropagationData(event); |
|
0 commit comments