Consider a SvelteKit page with a load function that throws an error. What will the user see by default?
export async function load() { throw new Error('Failed to load data'); }
Think about how SvelteKit handles errors thrown in load functions by default.
When a load function throws an error, SvelteKit automatically shows its default error page displaying the error message. This helps users know something went wrong.
Which load function properly catches an error and returns a custom error response to the page?
Remember that to return an error response from load, you must return an object with status and error properties.
Option A correctly catches the error and returns an object with status and error properties, which SvelteKit uses to render an error page with the given status and message.
Option A rethrows the error, which also works but does not return a custom error response object.
Options A and D return an error property without status, which SvelteKit does not interpret as an error response.
Given this load function, why does the page not show the error page when the fetch fails?
export async function load() { const res = await fetch('/api/data'); if (!res.ok) { return { error: new Error('Failed to fetch') }; } return { data: await res.json() }; }
Check what SvelteKit expects to show an error page from a load function.
SvelteKit requires the load function to return an object with status and error properties or throw an error to show the error page. Returning only an error property is ignored and treated as normal data.
error in the page when load returns a custom error?If the load function returns { status: 404, error: new Error('Not found') }, what will the error prop contain in the error page component?
Recall what type of value is passed as error to the error page.
The error prop in the error page receives the actual Error object returned from load. This allows the error page to access the message and stack trace.
Which approach correctly implements global error handling for all load functions in a SvelteKit app?
Think about SvelteKit's built-in error handling and how to customize error pages.
Option B is correct because SvelteKit uses a root +error.svelte component to display errors globally. Throwing errors in load functions triggers this component. Options A and B are manual and error-prone. Option B is invalid because hooks.server.js does not catch errors from load functions on the client side.