1
1
/* eslint-disable @sentry-internal/sdk/no-optional-chaining */
2
- import { getCurrentHub , trace } from '@sentry/core' ;
2
+ import { getCurrentHub , startSpan } from '@sentry/core' ;
3
3
import { captureException } from '@sentry/node' ;
4
4
import type { TransactionContext } from '@sentry/types' ;
5
5
import { addExceptionMechanism , addNonEnumerableProperty , objectify } from '@sentry/utils' ;
6
6
import type { LoadEvent , ServerLoadEvent } from '@sveltejs/kit' ;
7
7
8
8
import type { SentryWrappedFlag } from '../common/utils' ;
9
9
import { isHttpError , isRedirect } from '../common/utils' ;
10
- import { getTracePropagationData } from './utils' ;
10
+ import { flushIfServerless , getTracePropagationData } from './utils' ;
11
11
12
12
type PatchedLoadEvent = LoadEvent & SentryWrappedFlag ;
13
13
type PatchedServerLoadEvent = ServerLoadEvent & SentryWrappedFlag ;
@@ -57,7 +57,7 @@ function sendErrorToSentry(e: unknown): unknown {
57
57
// eslint-disable-next-line @typescript-eslint/no-explicit-any
58
58
export function wrapLoadWithSentry < T extends ( ...args : any ) => any > ( origLoad : T ) : T {
59
59
return new Proxy ( origLoad , {
60
- apply : ( wrappingTarget , thisArg , args : Parameters < T > ) => {
60
+ apply : async ( wrappingTarget , thisArg , args : Parameters < T > ) => {
61
61
// Type casting here because `T` cannot extend `Load` (see comment above function signature)
62
62
// Also, this event possibly already has a sentry wrapped flag attached
63
63
const event = args [ 0 ] as PatchedLoadEvent ;
@@ -80,7 +80,15 @@ export function wrapLoadWithSentry<T extends (...args: any) => any>(origLoad: T)
80
80
} ,
81
81
} ;
82
82
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
+ }
84
92
} ,
85
93
} ) ;
86
94
}
@@ -109,7 +117,7 @@ export function wrapLoadWithSentry<T extends (...args: any) => any>(origLoad: T)
109
117
// eslint-disable-next-line @typescript-eslint/no-explicit-any
110
118
export function wrapServerLoadWithSentry < T extends ( ...args : any ) => any > ( origServerLoad : T ) : T {
111
119
return new Proxy ( origServerLoad , {
112
- apply : ( wrappingTarget , thisArg , args : Parameters < T > ) => {
120
+ apply : async ( wrappingTarget , thisArg , args : Parameters < T > ) => {
113
121
// Type casting here because `T` cannot extend `ServerLoad` (see comment above function signature)
114
122
// Also, this event possibly already has a sentry wrapped flag attached
115
123
const event = args [ 0 ] as PatchedServerLoadEvent ;
@@ -144,7 +152,15 @@ export function wrapServerLoadWithSentry<T extends (...args: any) => any>(origSe
144
152
...traceparentData ,
145
153
} ;
146
154
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
+ }
148
164
} ,
149
165
} ) ;
150
166
}
0 commit comments