question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Thrown exceptions from __layout.svelte load result in plain text error responses

See original GitHub issue

Describe the bug

Throwing an exception (or returning a rejecting promise) from a load function should be equivalent to returning { status: 500, error } (apart from the former also resulting in a call to the handleError hook). However, currently, it results in a plain text error message response from the server, which should be returned only when the __error.svelte component fails to render.

Reproduction

Starting from the demo app, add this to __layout.svelte:

<script context="module">
	export function load() {
		throw new Error("FOO");
	}
</script>

Navigate to any page.

Logs

No response

System Info

System:
    OS: Linux 5.10 Ubuntu 20.04.3 LTS (Focal Fossa)
    CPU: (8) x64 Intel(R) Core(TM) i7-10510U CPU @ 1.80GHz
    Memory: 6.36 GB / 12.32 GB
    Container: Yes
    Shell: 5.0.17 - /bin/bash
  Binaries:
    Yarn: 1.22.15
    npm: 8.1.4
  npmPackages:
    @sveltejs/adapter-auto: next => 1.0.0-next.4
    @sveltejs/kit: next => 1.0.0-next.202
    svelte: ^3.44.0 => 3.44.3

Severity

serious, but I can work around it

Additional Information

This is inconvenient because I’d like to use the handleError hook as the sole place for performing certain kinds of error logging, but I’d also prefer to not return a plain text error response to users. This means that currently my __layout.svelte’s load function needs to catch the error and reimplement the handleError handling in a different way (since it has access to load arguments instead of to the request object).

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:13 (13 by maintainers)

github_iconTop GitHub Comments

2reactions
Rich-Harriscommented, Aug 27, 2022

It’s now possible to (group) your roots and put the root +error.svelte outside the group…

src/routes/
├ (main)
│ ├ foo/
│ ├ bar/
│ ├ etc/
│ ├ +layout.svelte # can safely throw errors in here
│ └ +page.svelte
├ +error.svelte   
└ +layout.svelte   # optional

…which mostly solves this issue. That said, there are always going to be cases where we need to bail out to something (e.g. the +error.svelte can’t render because it references data.undefined.something).

Right now, that’s a plain text response. I think we should make that a more appealing HTML page — something very basic, but nicer than what we currently have — and allow people to create a src/error.html page that overrides it, allowing you to at least render a branded page with a ‘visit our Twitter page for updates’ or whatever.

Unlike +error.svelte, we can guarantee that src/error.html can be rendered without errors.

Changing the plain text error response to HTML would be a breaking change, so I’ve added the label.

1reaction
Conduitrycommented, Jan 12, 2022

🤷 I mention in the “Additional Information” section specifically why I’m looking for this behavior. I currently have to duplicate some handleError hook logging logic in the __layout load.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Errors • Docs • SvelteKit
The exception is when the error occurs inside the root +layout.js or +layout.server.js , since the root layout would ordinarily contain the +error.svelte...
Read more >
Sveltekit returning a 404 on page load triggers an uncaught ...
Can anyone explain why an uncaught exception is thrown when returning a 404 status for a page on page load? And how best...
Read more >
Help with __error.svelte. `load({status, error})` returns ... - Reddit
My endpoints external API endpoints will return 402 responses if the user does not have a valid license. I want a top-level page...
Read more >
Edit Green Screen Videos - Media Jams
In this tutorial we'll be using Cloudinary and SvelteKit. SvelteKit is to Svelte.js just as Next.js is to React.js or Nuxt.js to Vue.js....
Read more >
Use Crash Reporting for ASP.NET - Raygun
Throw exceptions to avoid missing stack traces. Sometimes you may have code that detects that something has gone wrong, but doesn't actually throw...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found