Hint: Use throw error(status, message) in load [OK]
Common Mistakes:
Using new Error() with status code
Returning error object instead of throwing
Throwing plain objects without error helper
3. Given this load function, what will be the output if the fetch fails with a 500 status?
export async function load() {
try {
const res = await fetch('/api/data');
if (!res.ok) {
throw error(res.status, 'Failed to load data');
}
const data = await res.json();
return { data };
} catch (err) {
return { status: err.status || 500, error: err.message };
}
}
medium
A. { status: 500, error: 'Failed to load data' }
B. { data: undefined }
C. Page crashes with unhandled error
D. { status: 404, error: 'Not found' }
Solution
Step 1: Analyze fetch failure handling
If fetch returns a 500 status, res.ok is false, so throw error(500, 'Failed to load data') runs.
Step 2: Understand catch block behavior
The thrown error is caught, and the function returns an object with status and error from the error.
Final Answer:
{ status: 500, error: 'Failed to load data' } -> Option A
Quick Check:
Catch returns error object with status and message [OK]
Hint: Catch returns error object with status and message [OK]
Common Mistakes:
Expecting data to be returned on fetch failure
Assuming page crashes without catch
Confusing 404 with 500 status
4. Identify the error in this load function that causes the error handling to fail:
export async function load() {
try {
const res = await fetch('/api/data');
if (!res.ok) {
return error(res.status, 'Failed to load');
}
const data = await res.json();
return { data };
} catch (err) {
return { status: err.status, error: err.message };
}
}
medium
A. Not checking res.ok before parsing JSON
B. Missing await before fetch
C. Not returning data inside catch block
D. Using return error(...) instead of throw error(...)
Solution
Step 1: Check error throwing vs returning
The code uses return error(...) which does not throw an error but returns a value, so error handling is bypassed.
Step 2: Correct usage in SvelteKit
To trigger error handling, it must throw error(...) so the catch block or error page activates.
Final Answer:
Using return error(...) instead of throw error(...) -> Option D
Quick Check:
Throw error to activate error handling [OK]
Hint: Throw error(...) to trigger error handling, not return [OK]
Common Mistakes:
Returning error instead of throwing
Forgetting to await fetch
Ignoring res.ok check
5. You want to show a custom error page when your API returns a 403 status inside load. Which approach correctly handles this and passes the error to the page?
hard
A. Throw error(403, 'Access denied') inside load and create an error.svelte page to display it
B. Return { status: 403, error: 'Access denied' } and handle it manually in the page script
C. Use console.error and return empty data to show default page
D. Redirect to a different page using goto('/login') inside load
Solution
Step 1: Understand error propagation in load
Throwing error(403, 'Access denied') inside load tells SvelteKit to show the error page.
Step 2: Use error.svelte to customize error display
Creating an error.svelte page lets you show custom messages based on the error status and message.
Final Answer:
Throw error(403, 'Access denied') inside load and create an error.svelte page to display it -> Option A