kit: Rejecting streamed data

Describe the bug

When rejecting promises for streamed data, dev server usually crashes, and when it doesn’t crash it doesn’t show my custom error message.

Reproduction

https://stackblitz.com/edit/sveltejs-kit-template-default-2dz1kb?file=src/routes/+page.server.js

Logs

[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "Not Found".] {
  code: 'ERR_UNHANDLED_REJECTION'
}

System Info

System:
    OS: Linux 5.19 Ubuntu 22.04.2 LTS 22.04.2 LTS (Jammy Jellyfish)
    CPU: (12) x64 AMD Ryzen 5 5600X 6-Core Processor
    Memory: 7.30 GB / 15.53 GB
    Container: Yes
    Shell: 5.8.1 - /usr/bin/zsh
  Binaries:
    Node: 18.15.0 - ~/.nvm/versions/node/v18.15.0/bin/node
    npm: 9.5.0 - ~/.nvm/versions/node/v18.15.0/bin/npm
  Browsers:
    Firefox: 112.0.1
  npmPackages:
    @sveltejs/adapter-node: ^1.2.2 => 1.2.3 
    @sveltejs/kit: ^1.15.7 => 1.15.7 
    svelte: ^3.58.0 => 3.58.0 
    vite: ^4.3.1 => 4.3.1

Severity

serious, but I can work around it

Additional Information

No response

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 19
  • Comments: 28 (7 by maintainers)

Commits related to this issue

Most upvoted comments

This issue makes streamed data mostly unusable. Especially when dealing with fetches that may return a 404 or those that want to throw a redirect. It, for example, prevents us from using redirects to protect authentication sensitive routes.

And whats even worse is that the documentation of streamed data is suggesting that error handling isn’t an issue. The example promise won’t fail

            three: new Promise((fulfil) => {
                setTimeout(() => {
                    fulfil(3)
                }, 1000);
            })

and the error handling

    {#await data.streamed.three}
        Loading...
    {:then value}
        {value}
    {:catch error}
        {error.message}
    {/await}

suggests that rejected, streamed data does not crash the server.

Debugging this issue has cost us a lot of time that could have been prevent by mentioning this caveat in the documentation, or even better, implementing a fix. This should be a high priority issue, if you ask me.

@gtm-nayan - I think if this can’t be done for now, you guys need to update the docs explaining this clearly perhaps with an example workaround. If the await catch doesn’t work in the template at all, this shouldn’t be the example listed.

J

We’ll probably not be able to fix this without changing a few things in a major version because your promises can reject before they even reach SvelteKit.

In the meantime you can use workarounds https://jakearchibald.com/2023/unhandled-rejections/ to prevent crashing your server. Or set up a global promise rejection handler, which would be a good idea regardless of streamed promises.

For the ones that want to throw redirects from their streamed promises, that will not work, ever, because the browser has already received an OK response with the rest of the page by that point. The best you can do there is make another navigation on the client side like @eliasdefaria said.

Adding this snippet to the bottom of svelte.config.js will prevent crashes and help with debugging info during development

process.on('unhandledRejection', (reason, promise) => {
	console.error('Unhandled Rejection at: Promise', promise, 'reason:', reason);
});

Also the same problem, the funny thing is that the

{#await data.streamed.three}
      Loading...
  {:then value}
      {value}
  {:catch error}
      {error.message}
  {/await}

works when i navigate to this page from another route, BUT crash the server when i reload the page.

my workaround

Return data and error+code as normal data (like zod’s safeParse return) in server code, then transform it back in your .svelte file.

import { dev } from '$app/environment'
import { goto } from '$app/navigation'
import { isHttpError, isRedirect, type Redirect } from '@sveltejs/kit'

export type SafeStream<T> =
	| { data: T }
	| Redirect
	| { status: number; message: string }
export type StreamPromise<T> = Promise<SafeStream<T>>

export async function stream<T>(fn: () => Promise<T>): StreamPromise<T> {
	const { promise, resolve } = Promise.withResolvers<SafeStream<T>>()
	try {
		resolve({ data: await fn() })
	} catch (err: unknown) {
		if (isHttpError(err)) {
			const hideMessage = !dev && err.status === 500
			resolve({
				status: err.status,
				message: hideMessage ? 'Internal Error' : err.body.message,
			})
		} else if (isRedirect(err)) {
			resolve({ status: err.status, location: err.location })
		} else {
			throw err
		}
	}
	promise.catch(() => {}) // out of tick UnhandledPromiseRejection
	return promise
}

export async function unstream<T>(promise: StreamPromise<T>): Promise<T> {
	const { promise: p, resolve, reject } = Promise.withResolvers<T>()
	const result = await promise
	if ('status' in result) {
		if ('location' in result) {
			goto(result.location)
		} else {
			reject(new Error(result.message))
		}
	} else {
		resolve(result.data)
	}
	return p
}

// +layout.server.ts or +page.server.ts
import { stream } from '$lib/stream'

export const load: LayoutServerLoad = ({ locals }) => {
	return {
		apps: stream(() => myData()),
	}
}

// +page.svelte
<script lang="ts">
  let { data } = $props()
  const apps = unstream(data.apps)
</script>
  {#await data}
    Loading...
  {:then data}
    Here's data
  {:catch error}
    <p>error {error.message}</p>
  {/await}

rundown

Basically, if you want to stream promises:

  • in load, wrap that promise in a callback, pass that to stream()
  • in your .svelte page, unwrap that by passing to unstream()

overview

It’s not very elegant, but it’s type safe, it takes care of the next tick unhandled rejection problem and it lets you throw inside your promise returning functions that you want to stream without it killing your process (including calls to error, redirect).

As it is, errors other than those thrown by error or redirect are rethrown because those are unexpected to me.

redirects with streamed promise

If you want to handle redirects from the client side, you have to add code to unstream for that case

...
	if ('status' in result) {
		if (result.status >= 300 && result.status < 400) {
			goto('/login')
		} else {
			reject(result)
		}
	} else {
...

That’s not the greatest UX, to have a page fully load and then redirect to the login, but that is out of scope of this post.

redirects with awaited promise (non-streamed)

If you were to await the data, like this:

export const load: LayoutServerLoad = ({ locals }) => {
	return {
		apps: await myData(),
		// apps: stream(() => myData()),
	}
}

then, the redirect() would work as normally.

should we log or leave it to exit on unhandledRejections

I honestly don’t know. Maybe someone here can give us a hint.

To log, add this code to svelte.config.js:

process.on('unhandledRejection', (reason, promise) => {
	console.error('Unhandled Rejection at: Promise', promise, 'reason:', reason)
})

this is a workaround

I’ll be updating this as I see fit:

  • Svelte has a class HttpError, but it’s not exported, so I had to write StreamError.
  • I think I should hide the messages for 500 errors. I hate handling errors in typescript.
  • tabs are accidental

Let me know if someone has suggestions to improve this.

I also seem to be encountering this issue, hard to say if it was there before because I only noticed it once I was getting fetch timeouts from the API.

Rejecting a streaming promise will crash the dev server.

Add this to page.js in the starter template:

export const load = () => ({
	stream: {
		value: Promise.reject(42)
	}
});

The terminal prints:

[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "42".] {
  code: 'ERR_UNHANDLED_REJECTION'
}

This is a working reproduction for me:

https://stackblitz.com/edit/sveltejs-kit-template-default-lphmtx?file=src/routes/+page.js

SvelteKit can handle errors with streaming. You need to ensure two things:

  1. Use the fetch provided by the event, not the native one.
  2. Call the fetch inside the load method. Even if you pass the fetch to a custom fetch function, it will result in an error.

This approach works and triggers the {:catch} block in your template:

export const load: LayoutServerLoad = async ({ fetch }) => { const session = await event.locals.auth(); return { demo: fetch(‘/api/v1/users’, { method: ‘GET’ }) .then((res) => { if (!res.ok) { error(res.status); } else { return res.json(); } }) }; };

This was very helpful and managed to work for me - thanks @yaman-cruise. Despite the fact that I am using the provided fetch from event, if I do not add the catch no-op then I get the following error:

UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "[object Object]".
    at throwUnhandledRejectionsMode (node:internal/process/promises:392:7)
    at processPromiseRejections (node:internal/process/promises:475:17)
    at process.processTicksAndRejections (node:internal/process/task_queues:106:32) {
  code: 'ERR_UNHANDLED_REJECTION'
}

The docs seem to infer that this is not necessary when using even.fetch, but for whatever reason it was for me.

I also found it crucial that you cannot chain the catch to the then. The following works:

const resource = fetch('/resource').then((res) => {
    if (res.ok) return res.json();
    else error(res.status, { message: res.statusText, status: res.status });
  });
  resource.catch(() => {});

But this does not:

  const resource = fetch('/resource')
    .then((res) => {
      if (res.ok) return res.json();
      else error(res.status, { message: res.statusText, status: res.status });
    })
    .catch(() => {});

Similarly you can’t do:

  const resource = fetch('/resource');
  resource.then((res) => {
    if (res.ok) return res.json();
    else error(res.status, { message: res.statusText, status: res.status });
  });

On the client side I am now able to access all values either using

$effect(() => data.resource.then().catch())

or

{#await data.resource}
  loading resource
{:then resource}
  {JSON.stringify(resource, null, 2)}
{:catch error}
  <p>there has been an error</p>
  <pre>{JSON.stringify(error, null, 2)}</pre>
{/await}

Hopefully this helps someone else. If anyone knows why I still need the no-op catch even when using event.fetch I would be interested to understand more.

SvelteKit can handle errors with streaming. You need to ensure two things:

  1. Use the fetch provided by the event, not the native one.
  2. Call the fetch inside the load method. Even if you pass the fetch to a custom fetch function, it will result in an error.

This approach works and triggers the {:catch} block in your template:

export const load: LayoutServerLoad = async ({ fetch }) => {
	const session = await event.locals.auth();
	return {
		demo: fetch('/api/v1/users', {
				method: 'GET'
			})
			.then((res) => {
				if (!res.ok) {
					error(res.status);
				} else {
					return res.json();
				}
			})
	};
};

This doesn’t work because fetch is not called inside the load function:

const fetchUsers = async ({ fetch }: ServerLoadEvent) => {
	const res = await fetch('/api/supply/v1/account', {
		method: 'GET'
	});
	if (!res.ok) {
		error(res.status);
	} else {
		return (await res.json()) as Account;
	}
};
export const load: LayoutServerLoad = async ({ fetch }) => {
	const session = await event.locals.auth();
	return {
		demo: fetchUsers(fetch)
	};
};

All of this is mentioned in the documentation: https://svelte.dev/docs/kit/load#Streaming-with-promises. I believe we can close this ticket.

@gtm-nayan is there a solution for this in svelte 5? This comment is still true, https://github.com/sveltejs/kit/issues/9785#issuecomment-1552836871, the documentation is still misleading.

We’ll probably not be able to fix this without changing a few things in a major version because your promises can reject before they even reach SvelteKit.

In the meantime you can use workarounds https://jakearchibald.com/2023/unhandled-rejections/ to prevent crashing your server. Or set up a global promise rejection handler, which would be a good idea regardless of streamed promises.

For the ones that want to throw redirects from their streamed promises, that will not work, ever, because the browser has already received an OK response with the rest of the page by that point. The best you can do there is make another navigation on the client side like @eliasdefaria said.

Thank you for the workaround, but I am here from my first Svelte (and JS) project, and that seems a bit complex for me.

A friend helped and I got this working. Hope it helps some newer people like me who want to use streaming promises without having the whole app crash and need a more basic workaround.

in +page.server.ts

export const load: PageServerLoad = async ({ params, locals }) => {
	const res = fetch(`http://localhost:8000/api/myendpoint`);
	return { results: { streamed: res
			.then(
				(resp) => {
					if (resp.status === 200) {
						return resp.json()
					} else if (resp.status === 404) {
						console.log("App Not found");
						return "Not Found"
					} else if (resp.status === 500) {
						console.log("API Server error");
						return "Backend Error"
					}
				}
			)
			.then(
				json=> json,
				error => {console.log('Uncaught error', error); return "Uncaught Error"}
			)
		}};
};

Then in my +page.svelte

{#await data.results.streamed}
	Loading ...
{:then results}
	{#if typeof results != 'string'}
		Loaded!
		<!-- <AppGroupCard apps={results} /> -->
	{:else}
		<p>Search failed please try again ... {results}</p>
	{/if}
{:catch error}
	<!-- NOTE: This is currently not displaying -->
	<p>Search failed please try again ... {error.message}</p>
{/await}

Bump, I spent way too much time trying to find a solution to this issue. Only thanks @gtarsia 's solution was I able to finally get it to work.

Please document this somewhere or link to this issue!

this is what I am using to resolve this issue

function streamedError(err: {status: number, body: {message: string}}) { return new Promise((fulfil, reject) => { setTimeout(() => { reject(error(err.status, err.body.message)); }, 500); }); }

and then I attach it to the streamed promises as follows: streamedFunc().catch(err=>streamedError(err))

@adrifer Before you tread down that path, I should let you know that you can’t throw redirects from a streamed promise. The server needs to know the status code before it starts sending the response and it’s too late by the time it gets to your streamed promises.

As for the issue in general, Node.js requires that an error handler must be attached before the tick in which the promise is rejected. In SvelteKit, that’s done by the serializer, but by the time we’re done awaiting all the load functions it may have been too late to attach the handler.

I can’t think of a clean way to solve this yet, I don’t think it’s actually possible with the current API, we’d have to walk the returned objects (arrays, maps, whatever included) as soon as we get it from the user but turns out even that’s not enough.

Consider

{ nested: { promise: Promise.reject() }, something: await setTimeout(1000) }

the rejected promise is created before the yield point of the timeout so the exception is triggered and the process exits before the promise even reaches SvelteKit.

Perhaps there is some way to kludge this, ensuring we attach the error handlers before any other yield point is reached. ~The handlers would catch the error and put it back on the promise under some special key and the serializer checks the key to get errors that happened before the promise reached the serializer~ (apparently just need to chain a catch handler, and not use the chained promise) but I won’t be too hopeful that it’s viable. We might need something like defer() after all.

Cannot reproduce the crash with the provided repro on either stackblitz or locally, but can confirm the error reporting seems to be broken. It’s logging undefined every time.