|
1 | 1 | /* eslint-disable @sentry-internal/sdk/no-optional-chaining */
|
2 |
| -import { trace } from '@sentry/core'; |
3 | 2 | import { captureException } from '@sentry/node';
|
4 |
| -import { |
5 |
| - addExceptionMechanism, |
6 |
| - baggageHeaderToDynamicSamplingContext, |
7 |
| - extractTraceparentData, |
8 |
| - objectify, |
9 |
| -} from '@sentry/utils'; |
10 |
| -import type { HttpError, ServerLoad } from '@sveltejs/kit'; |
| 3 | +import { addExceptionMechanism, isThenable, objectify } from '@sentry/utils'; |
| 4 | +import type { HttpError, Load, ServerLoad } from '@sveltejs/kit'; |
11 | 5 | import * as domain from 'domain';
|
12 | 6 |
|
13 | 7 | function isHttpError(err: unknown): err is HttpError {
|
@@ -49,32 +43,26 @@ function sendErrorToSentry(e: unknown): unknown {
|
49 | 43 | *
|
50 | 44 | * @param origLoad SvelteKit user defined load function
|
51 | 45 | */
|
52 |
| -export function wrapLoadWithSentry(origLoad: ServerLoad): ServerLoad { |
| 46 | +export function wrapLoadWithSentry<T extends ServerLoad | Load>(origLoad: T): T { |
53 | 47 | return new Proxy(origLoad, {
|
54 | 48 | apply: (wrappingTarget, thisArg, args: Parameters<ServerLoad>) => {
|
55 | 49 | return domain.create().bind(() => {
|
56 |
| - const [event] = args; |
| 50 | + let maybePromiseResult: ReturnType<T>; |
57 | 51 |
|
58 |
| - const sentryTraceHeader = event.request.headers.get('sentry-trace'); |
59 |
| - const baggageHeader = event.request.headers.get('baggage'); |
60 |
| - const traceparentData = sentryTraceHeader ? extractTraceparentData(sentryTraceHeader) : undefined; |
61 |
| - const dynamicSamplingContext = baggageHeaderToDynamicSamplingContext(baggageHeader); |
| 52 | + try { |
| 53 | + maybePromiseResult = wrappingTarget.apply(thisArg, args); |
| 54 | + } catch (e) { |
| 55 | + sendErrorToSentry(e); |
| 56 | + throw e; |
| 57 | + } |
62 | 58 |
|
63 |
| - const routeId = event.route.id; |
64 |
| - return trace( |
65 |
| - { |
66 |
| - op: 'function.sveltekit.load', |
67 |
| - name: routeId ? routeId : event.url.pathname, |
68 |
| - status: 'ok', |
69 |
| - ...traceparentData, |
70 |
| - metadata: { |
71 |
| - source: routeId ? 'route' : 'url', |
72 |
| - dynamicSamplingContext: traceparentData && !dynamicSamplingContext ? {} : dynamicSamplingContext, |
73 |
| - }, |
74 |
| - }, |
75 |
| - () => wrappingTarget.apply(thisArg, args), |
76 |
| - sendErrorToSentry, |
77 |
| - ); |
| 59 | + if (isThenable(maybePromiseResult)) { |
| 60 | + Promise.resolve(maybePromiseResult).then(null, e => { |
| 61 | + sendErrorToSentry(e); |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + return maybePromiseResult; |
78 | 66 | })();
|
79 | 67 | },
|
80 | 68 | });
|
|
0 commit comments