File tree Expand file tree Collapse file tree 1 file changed +18
-2
lines changed
packages/sveltekit/src/server-common Expand file tree Collapse file tree 1 file changed +18
-2
lines changed Original file line number Diff line number Diff line change 1
- import { captureException , consoleSandbox } from '@sentry/core' ;
1
+ import { captureException , consoleSandbox , flush } from '@sentry/core' ;
2
2
import type { HandleServerError } from '@sveltejs/kit' ;
3
3
import { flushIfServerless } from '../server-common/utils' ;
4
4
@@ -42,7 +42,23 @@ export function handleErrorWithSentry(handleError: HandleServerError = defaultEr
42
42
} ,
43
43
} ) ;
44
44
45
- await flushIfServerless ( ) ;
45
+ const platform = input . event . platform as {
46
+ context ?: {
47
+ waitUntil ?: ( fn : ( ) => Promise < void > ) => void ;
48
+ } ;
49
+ } ;
50
+
51
+ // Cloudflare workers have a `waitUntil` method that we can use to flush the event queue
52
+ // We already call this in `wrapRequestHandler` from `sentryHandleInitCloudflare`
53
+ // However, `handleError` can be invoked when wrapRequestHandler already finished
54
+ // (e.g. when responses are streamed / returning promises from load functions)
55
+ const cloudflareWaitUntil = platform ?. context ?. waitUntil ;
56
+ if ( typeof cloudflareWaitUntil === 'function' ) {
57
+ const waitUntil = cloudflareWaitUntil . bind ( platform . context ) ;
58
+ waitUntil ( flush ( 2000 ) ) ;
59
+ } else {
60
+ await flushIfServerless ( ) ;
61
+ }
46
62
47
63
// We're extra cautious with SafeHandleServerErrorInput - this type is not compatible with HandleServerErrorInput
48
64
// @ts -expect-error - we're still passing the same object, just with a different (backwards-compatible) type
You can’t perform that action at this time.
0 commit comments