Skip to content

fix(sveltekit): Ensure server errors from streamed responses are sent #17044

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 17, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions packages/sveltekit/src/server-common/handleError.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { captureException, consoleSandbox } from '@sentry/core';
import { captureException, consoleSandbox, flush } from '@sentry/core';
import type { HandleServerError } from '@sveltejs/kit';
import { flushIfServerless } from '../server-common/utils';

Expand Down Expand Up @@ -42,7 +42,23 @@ export function handleErrorWithSentry(handleError: HandleServerError = defaultEr
},
});

await flushIfServerless();
const platform = input.event.platform as {
context?: {
waitUntil?: (fn: () => Promise<void>) => void;
};
};

// Cloudflare workers have a `waitUntil` method that we can use to flush the event queue
// We already call this in `wrapRequestHandler` from `sentryHandleInitCloudflare`
// However, `handleError` can be invoked when wrapRequestHandler already finished
// (e.g. when responses are streamed / returning promises from load functions)
const cloudflareWaitUntil = platform?.context?.waitUntil;
if (typeof cloudflareWaitUntil === 'function') {
const waitUntil = cloudflareWaitUntil.bind(platform.context);
waitUntil(flush(2000));
} else {
await flushIfServerless();
}

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