11/* eslint-disable @sentry-internal/sdk/no-optional-chaining */
2- import { getCurrentHub , trace } from '@sentry/core' ;
2+ import { getCurrentHub , startSpan } from '@sentry/core' ;
33import { captureException } from '@sentry/node' ;
44import type { TransactionContext } from '@sentry/types' ;
55import { addExceptionMechanism , addNonEnumerableProperty , objectify } from '@sentry/utils' ;
66import type { LoadEvent , ServerLoadEvent } from '@sveltejs/kit' ;
77
88import type { SentryWrappedFlag } from '../common/utils' ;
99import { isHttpError , isRedirect } from '../common/utils' ;
10- import { getTracePropagationData } from './utils' ;
10+ import { flushIfServerless , getTracePropagationData } from './utils' ;
1111
1212type PatchedLoadEvent = LoadEvent & SentryWrappedFlag ;
1313type PatchedServerLoadEvent = ServerLoadEvent & SentryWrappedFlag ;
@@ -57,7 +57,7 @@ function sendErrorToSentry(e: unknown): unknown {
5757// eslint-disable-next-line @typescript-eslint/no-explicit-any
5858export function wrapLoadWithSentry < T extends ( ...args : any ) => any > ( origLoad : T ) : T {
5959 return new Proxy ( origLoad , {
60- apply : ( wrappingTarget , thisArg , args : Parameters < T > ) => {
60+ apply : async ( wrappingTarget , thisArg , args : Parameters < T > ) => {
6161 // Type casting here because `T` cannot extend `Load` (see comment above function signature)
6262 // Also, this event possibly already has a sentry wrapped flag attached
6363 const event = args [ 0 ] as PatchedLoadEvent ;
@@ -80,7 +80,15 @@ export function wrapLoadWithSentry<T extends (...args: any) => any>(origLoad: T)
8080 } ,
8181 } ;
8282
83- return trace ( traceLoadContext , ( ) => wrappingTarget . apply ( thisArg , args ) , sendErrorToSentry ) ;
83+ try {
84+ // We need to await before returning, otherwise we won't catch any errors thrown by the load function
85+ return await startSpan ( traceLoadContext , ( ) => wrappingTarget . apply ( thisArg , args ) ) ;
86+ } catch ( e ) {
87+ sendErrorToSentry ( e ) ;
88+ throw e ;
89+ } finally {
90+ await flushIfServerless ( ) ;
91+ }
8492 } ,
8593 } ) ;
8694}
@@ -109,7 +117,7 @@ export function wrapLoadWithSentry<T extends (...args: any) => any>(origLoad: T)
109117// eslint-disable-next-line @typescript-eslint/no-explicit-any
110118export function wrapServerLoadWithSentry < T extends ( ...args : any ) => any > ( origServerLoad : T ) : T {
111119 return new Proxy ( origServerLoad , {
112- apply : ( wrappingTarget , thisArg , args : Parameters < T > ) => {
120+ apply : async ( wrappingTarget , thisArg , args : Parameters < T > ) => {
113121 // Type casting here because `T` cannot extend `ServerLoad` (see comment above function signature)
114122 // Also, this event possibly already has a sentry wrapped flag attached
115123 const event = args [ 0 ] as PatchedServerLoadEvent ;
@@ -144,7 +152,15 @@ export function wrapServerLoadWithSentry<T extends (...args: any) => any>(origSe
144152 ...traceparentData ,
145153 } ;
146154
147- return trace ( traceLoadContext , ( ) => wrappingTarget . apply ( thisArg , args ) , sendErrorToSentry ) ;
155+ try {
156+ // We need to await before returning, otherwise we won't catch any errors thrown by the load function
157+ return await startSpan ( traceLoadContext , ( ) => wrappingTarget . apply ( thisArg , args ) ) ;
158+ } catch ( e : unknown ) {
159+ sendErrorToSentry ( e ) ;
160+ throw e ;
161+ } finally {
162+ await flushIfServerless ( ) ;
163+ }
148164 } ,
149165 } ) ;
150166}
0 commit comments