Discover how a simple file can turn frustrating errors into helpful guides for your users!
Why Error pages (+error.svelte)? - Purpose & Use Cases
Imagine your website suddenly shows a blank screen or confusing messages when something goes wrong, like a missing page or server error.
Users get lost and frustrated because they don't know what happened or what to do next.
Manually handling errors means writing lots of repeated code everywhere to catch problems.
This is slow, easy to forget, and often leads to inconsistent or ugly error messages that confuse users.
The +error.svelte file in SvelteKit lets you create one place to show friendly, clear error pages automatically whenever something goes wrong.
This keeps your app clean and users informed without extra work everywhere.
try { fetchData() } catch { showAlert('Error!') }
<script>
export let status;
export let error;
</script>
<h1>{status}</h1>
<p>{error.message}</p>You can provide smooth, helpful error experiences that guide users back on track without breaking your app's flow.
When a user visits a broken link, instead of a confusing blank page, they see a friendly message like "Oops! Page not found" with a button to go home.
Error pages improve user experience by handling problems gracefully.
+error.svelte centralizes error display in SvelteKit apps.
This saves time and keeps your app consistent and user-friendly.
Practice
+error.svelte file in a SvelteKit project?Solution
Step 1: Understand the role of
This file is specifically designed to show error messages when something goes wrong in the app.+error.svelteStep 2: Compare with other options
Authentication, styling, and routing are handled elsewhere, not in+error.svelte.Final Answer:
To display a custom error page when an error occurs in the app -> Option BQuick Check:
Error page =+error.svelte[OK]
+error.svelte shows errors, not routes or styles [OK]- Confusing error page with routing or styling files
- Thinking
+error.sveltehandles authentication - Assuming it is for normal page content
+error.svelte?Solution
Step 1: Recall SvelteKit error props
In+error.svelte, thestatusis passed as a separate prop, not insideerror.Step 2: Check each option
{error.status}is incorrect becauseerrorholds the error object, but status is separate.{status}is correct.{error.message}is for the message, not status.{params.status}is unrelated.Final Answer:
{status} -> Option DQuick Check:
Status code ={status}[OK]
- Using {error.status} instead of {status}
- Confusing error message with status code
- Trying to access status from params
+error.svelte snippet:<script>
export let error;
export let status;
</script>
<h1>Error {status}</h1>
<p>{error.message}</p>What will be displayed if
status is 404 and error.message is "Page not found"?Solution
Step 1: Substitute values in template
The{status}is 404, so <h1> shows "Error 404". The{error.message}is "Page not found", so <p> shows that text.Step 2: Confirm output matches option
Error 404
Page not found matches exactly: "Error 404" and "Page not found". Others have wrong order or undefined values.Final Answer:
Error 404
Page not found -> Option AQuick Check:
Display status and message correctly [OK]
- Mixing status and message order
- Assuming undefined values
- Confusing error.message with status
+error.svelte code:<script>
export let error;
</script>
<h1>Error {status}</h1>
<p>{error.message}</p>But when an error occurs, the page shows
Error undefined. What is the problem?Solution
Step 1: Identify missing prop export
The code uses{status}but does not exportstatusfrom the script, so it is undefined.Step 2: Check other options
{error.status}is incorrect because status is a separate prop. Importing status is not needed.error.messageexists, so that is not the issue.Final Answer:
You forgot to export thestatusprop -> Option CQuick Check:
Export all props used in template [OK]
- Using {status} without exporting it
- Trying to access status inside error object
- Assuming error.message is missing
+error.svelte code correctly implements this?Solution
Step 1: Check how status is used
Status is a number, so comparingstatus === 404is correct.<script> export let error; export let status; </script> <h1>Error {status}</h1> <p>{status === 404 ? 'Page not found' : error.message}</p>and D do this, but D compares to string '404' which is wrong.Step 2: Check error.message usage
<script> export let error; export let status; </script> <h1>Error {status}</h1> <p>{status === 404 ? 'Page not found' : error.message}</p>useserror.messageas fallback, which is correct. Options B and C incorrectly compareerror.messageto 404, which makes no sense.Step 3: Confirm correct syntax and logic
<script> export let error; export let status; </script> <h1>Error {status}</h1> <p>{status === 404 ? 'Page not found' : error.message}</p>correctly exports both props and uses a ternary to show special message for 404, else shows error.message.Final Answer:
Option A code correctly handles 404 and other errors -> Option AQuick Check:
Compare status number, not error.message [OK]
- Comparing error.message to 404 instead of status
- Comparing status to string '404' instead of number
- Not exporting both status and error props
Ready to prove it?
Pass the test and this pattern moves unknown
