0
0
Svelteframework~20 mins

Error handling in load in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Error Handling Mastery in SvelteKit
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when load throws an error?

Consider a SvelteKit page with a load function that throws an error. What will the user see by default?

Svelte
export async function load() {
  throw new Error('Failed to load data');
}
AThe page shows a default error page with the error message.
BThe page silently fails and shows no content.
CThe page reloads infinitely until the error is fixed.
DThe page shows the previous cached content without error.
Attempts:
2 left
💡 Hint

Think about how SvelteKit handles errors thrown in load functions by default.

📝 Syntax
intermediate
2:00remaining
Which load function correctly catches and returns an error?

Which load function properly catches an error and returns a custom error response to the page?

A
export async function load() {
  try {
    const res = await fetch('/api/data');
    if (!res.ok) throw new Error('Fetch failed');
    return { data: await res.json() };
  } catch (error) {
    return { status: 500, error: new Error('Custom load error') };
  }
}
B
export async function load() {
  const res = await fetch('/api/data');
  if (!res.ok) return { error: 'Fetch failed' };
  return { data: await res.json() };
}
C
export async function load() {
  try {
    const res = await fetch('/api/data');
    if (!res.ok) throw new Error('Fetch failed');
    return { data: await res.json() };
  } catch (error) {
    throw new Error('Custom load error');
  }
}
D
export async function load() {
  try {
    const res = await fetch('/api/data');
    if (!res.ok) throw new Error('Fetch failed');
    return { data: await res.json() };
  } catch (error) {
    return { error: 'Custom load error' };
  }
}
Attempts:
2 left
💡 Hint

Remember that to return an error response from load, you must return an object with status and error properties.

🔧 Debug
advanced
2:00remaining
Why does this load function not show the error page?

Given this load function, why does the page not show the error page when the fetch fails?

Svelte
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() };
}
ABecause the <code>load</code> function must throw the error instead of returning it.
BBecause the fetch never fails, so the error block is never reached.
CBecause returning an <code>error</code> property without <code>status</code> does not trigger the error page.
DBecause the <code>load</code> function must return a rejected promise to trigger the error page.
Attempts:
2 left
💡 Hint

Check what SvelteKit expects to show an error page from a load function.

state_output
advanced
2:00remaining
What is the value of 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?

AA string 'Not found'.
BUndefined, because error is not passed as a prop.
CAn object with a <code>message</code> property containing 'Not found'.
DAn Error object with message 'Not found'.
Attempts:
2 left
💡 Hint

Recall what type of value is passed as error to the error page.

🧠 Conceptual
expert
3:00remaining
How to handle errors globally in SvelteKit load functions?

Which approach correctly implements global error handling for all load functions in a SvelteKit app?

AWrap every <code>load</code> function in try-catch and return error objects manually in each page.
BCreate a root <code>+error.svelte</code> component to customize error display and throw errors in <code>load</code> functions as needed.
CUse a global <code>hooks.server.js</code> file to catch errors thrown in <code>load</code> functions and return custom responses.
DSet a global variable to track errors and check it in each page's <code>load</code> function.
Attempts:
2 left
💡 Hint

Think about SvelteKit's built-in error handling and how to customize error pages.