Skip to content
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

Trouble catching fetch errors in module context load #4659

Closed
stephenlrandall opened this issue Apr 19, 2022 · 3 comments
Closed

Trouble catching fetch errors in module context load #4659

stephenlrandall opened this issue Apr 19, 2022 · 3 comments
Labels
bug Something isn't working
Milestone

Comments

@stephenlrandall
Copy link
Contributor

stephenlrandall commented Apr 19, 2022

Describe the bug

Adding module context load functions with fetch attempts to non-existent resources seems to introduce errors that can't be caught. Similar fetch calls in the non-module context script tags can have their errors caught in the expected way:

res = await fetch('/resource/no/exist').catch(err => ...);

Additionally, fetch errors in __layout.svelte specifically will cause an infinite loop as the load function is run over and over again.

Reproduction

In the template SvelteKit to-do application, add something to the effect of:

<script context="module">
	export async function load({ fetch }) {
		console.log("Attempting to fetch...");

		const res = await fetch("/-favicon.png").catch(_ => console.log("Caught an error!"))

		if (res.ok)
			return { status: 200 };
		else
			return {
				status: res.status,
				props: { error: res.statusText }
			};
	}
</script>

If this is in __layout.svelte, the "Attempting to fetch..." line will be printed indefinitely as the load hangs. If this is in a specific route (like the About page), navigation will not hang but the error will also not get caught.

If this fetch is not in module context, the error is caught and the "Caught an error!" line is printed. If the fetch is changed to an existing resource ("-favicon" -> "favicon", for example), everything works fine as well.

Logs

No response

System Info

System:
    OS: macOS 12.3.1
    CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 780.53 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 17.9.0 - /usr/local/bin/node
    npm: 8.5.5 - /usr/local/bin/npm
  Browsers:
    Safari: 15.4
  npmPackages:
    @sveltejs/adapter-auto: next => 1.0.0-next.34 
    @sveltejs/kit: next => 1.0.0-next.314 
    svelte: ^3.46.0 => 3.47.0

Severity

serious, but I can work around it

Additional Information

I can work around this issue by moving the fetch calls into the main script to correctly catch any errors, but this is obviously non-ideal as content will flash/jump as it's loaded in.

Really hoping this is me missing something obvious, and apologies if that's the case! I tried asking about this in the Svelte Discord a couple times but got no response.

@plmrry
Copy link

plmrry commented Apr 19, 2022

Isn't this expected behavior? fetch does not throw on 404s.

@stephenlrandall
Copy link
Contributor Author

Isn't this expected behavior? fetch does not throw on 404s.

Surely an infinitely looping load function is not expected behavior though? Is there a different way I'm meant to be handling that situation?

Rich-Harris added a commit that referenced this issue Apr 19, 2022
@Rich-Harris Rich-Harris added the bug Something isn't working label May 12, 2022
@Rich-Harris Rich-Harris added this to the 1.0 milestone May 12, 2022
Rich-Harris added a commit that referenced this issue Jul 8, 2022
Rich-Harris added a commit that referenced this issue Jul 11, 2022
Rich-Harris added a commit that referenced this issue Jul 14, 2022
Rich-Harris added a commit that referenced this issue Jul 14, 2022
* failing test for #4659

* prevent infinite loop when loading non-existent route in __layout

* update test

* tighten up

* changeset

* Update .changeset/purple-rats-wink.md

* wut
@Rich-Harris
Copy link
Member

closed via #4665 — thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants