Create a Custom Error Page with +error.svelte in SvelteKit
📖 Scenario: You are building a website using SvelteKit. You want to show a friendly error page whenever something goes wrong, like a missing page or a server error.
🎯 Goal: Create a custom error page using the +error.svelte file that shows the error status and message clearly to the user.
📋 What You'll Learn
Create a +error.svelte file with a script block that receives error and status props
Display the error status and message in the page content
Add a friendly heading and a short explanation
Use semantic HTML and accessible roles
Style the error page with simple CSS for clarity and responsiveness
💡 Why This Matters
🌍 Real World
Custom error pages improve user experience by showing friendly messages when something goes wrong on a website.
💼 Career
Knowing how to create error pages is important for frontend developers working with SvelteKit or similar frameworks to handle errors gracefully.
Progress0 / 4 steps
1
Create the basic +error.svelte file with props
Create a +error.svelte file. Inside it, write a <script> block that declares export let error and export let status to receive error details.
Svelte
Hint
Use export let to declare props in Svelte components.
2
Add HTML structure to display error status and message
Below the <script> block, add a <main> element with a heading <h1> that shows Error {status}. Then add a paragraph <p> that displays {error.message}.
Svelte
Hint
Use curly braces {} to insert variables in Svelte templates.
3
Add a friendly explanation and semantic roles
Inside the <main>, add a second paragraph <p> with the text Sorry, something went wrong. Also add role="alert" to the <main> element for accessibility.
Svelte
Hint
Use role="alert" on the main container to notify screen readers of important messages.
4
Add simple CSS styles for clarity and responsiveness
Add a <style> block below the HTML. Inside it, style main with max-width: 30rem;, margin: 2rem auto;, padding: 1rem;, border: 1px solid #ccc;, and border-radius: 0.5rem;. Also style h1 with color: #c00; and font-size: 2rem;.
Svelte
Hint
Use CSS properties inside a <style> block to style your error page.
Practice
(1/5)
1. What is the main purpose of the +error.svelte file in a SvelteKit project?
easy
A. To handle user authentication
B. To display a custom error page when an error occurs in the app
C. To define global styles for the app
D. To manage routing between pages
Solution
Step 1: Understand the role of +error.svelte
This file is specifically designed to show error messages when something goes wrong in the app.
Step 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 B
Quick Check:
Error page = +error.svelte [OK]
Hint: Remember: +error.svelte shows errors, not routes or styles [OK]
Common Mistakes:
Confusing error page with routing or styling files
Thinking +error.svelte handles authentication
Assuming it is for normal page content
2. Which of the following is the correct way to access the HTTP status code inside +error.svelte?
easy
A. {error.status}
B. {params.status}
C. {error.message}
D. {status}
Solution
Step 1: Recall SvelteKit error props
In +error.svelte, the status is passed as a separate prop, not inside error.
Step 2: Check each option
{error.status} is incorrect because error holds 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 D
Quick Check:
Status code = {status} [OK]
Hint: Status is a separate prop, not inside error object [OK]
Common Mistakes:
Using {error.status} instead of {status}
Confusing error message with status code
Trying to access status from params
3. Given this +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"?
medium
A. Error 404 Page not found
B. Error Page not found 404
C. Error 0 undefined
D. Error 404 undefined
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 A
Quick Check:
Display status and message correctly [OK]
Hint: Match variable names exactly to output text [OK]
Common Mistakes:
Mixing status and message order
Assuming undefined values
Confusing error.message with status
4. You wrote this +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?
medium
A. You need to import status from another file
B. You should use {error.status} instead of {status}
C. You forgot to export the status prop
D. The error object does not have a message property
Solution
Step 1: Identify missing prop export
The code uses {status} but does not export status from 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.message exists, so that is not the issue.
Final Answer:
You forgot to export the status prop -> Option C
Quick Check:
Export all props used in template [OK]
Hint: Always export props you use in template [OK]
Common Mistakes:
Using {status} without exporting it
Trying to access status inside error object
Assuming error.message is missing
5. You want to create a custom error page that shows a special message for 404 errors and a generic message for others. Which +error.svelte code correctly implements this?
hard
A.
<script>
export let error;
export let status;
</script>
<h1>Error {status}</h1>
<p>{status === 404 ? 'Page not found' : error.message}</p>
B.
<script>
export let error;
</script>
<h1>Error {error.status}</h1>
<p>{error.message === 404 ? 'Page not found' : error.message}</p>
C.
<script>
export let error;
export let status;
</script>
<h1>Error {status}</h1>
<p>{error.message === 404 ? 'Page not found' : error.message}</p>
D.
<script>
export let error;
export let status;
</script>
<h1>Error {status}</h1>
<p>{status === '404' ? 'Page not found' : error.message}</p>
Solution
Step 1: Check how status is used
Status is a number, so comparing status === 404 is 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>
uses error.message as fallback, which is correct. Options B and C incorrectly compare error.message to 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 A
Quick Check:
Compare status number, not error.message [OK]
Hint: Compare status number with 404, not error.message [OK]
Common Mistakes:
Comparing error.message to 404 instead of status
Comparing status to string '404' instead of number
Not exporting both status and error props
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.