Skip to content

Commit 7550f05

Browse files
committed
fix(sveltekit): Ensure errors from streamed responses are sent
1 parent 1b41126 commit 7550f05

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

packages/sveltekit/src/server-common/handleError.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { captureException, consoleSandbox } from '@sentry/core';
1+
import { captureException, consoleSandbox, flush } from '@sentry/core';
22
import type { HandleServerError } from '@sveltejs/kit';
33
import { flushIfServerless } from '../server-common/utils';
44

@@ -42,7 +42,23 @@ export function handleErrorWithSentry(handleError: HandleServerError = defaultEr
4242
},
4343
});
4444

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+
}
4662

4763
// We're extra cautious with SafeHandleServerErrorInput - this type is not compatible with HandleServerErrorInput
4864
// @ts-expect-error - we're still passing the same object, just with a different (backwards-compatible) type

0 commit comments

Comments
 (0)