Error pages help users understand when something goes wrong on a website. They show friendly messages instead of confusing errors.
Error pages (+error.svelte)
<script> export let error; export let status; </script> <h1>{status}</h1> <p>{error.message}</p>
+error.svelte file is a special SvelteKit file that shows error pages automatically.error and status props to display error details.<script> export let error; export let status; </script> <h1>Error {status}</h1> <p>{error.message}</p>
<script> export let error; export let status; </script> <h1>Oops! Something went wrong.</h1> <p>Status: {status}</p> <p>{error.message}</p>
<script> export let error; export let status; </script> <style> h1 { color: red; } p { font-style: italic; } </style> <h1>Error {status}</h1> <p>{error.message}</p>
This is a complete +error.svelte file for SvelteKit. It shows the error status and message in a centered box. It uses semantic HTML and accessibility features like role="alert" and aria-live="assertive" so screen readers announce the error immediately.
<script> export let error; export let status; </script> <style> main { max-width: 600px; margin: 3rem auto; padding: 1rem; font-family: system-ui, sans-serif; text-align: center; } h1 { color: #b00020; font-size: 3rem; } p { font-size: 1.25rem; color: #333; } </style> <main role="alert" aria-live="assertive"> <h1>Error {status}</h1> <p>{error.message}</p> </main>
The +error.svelte file automatically catches errors from your routes.
You can customize the error page to match your site's style and tone.
Always use semantic HTML and accessibility attributes to help all users understand the error.
Error pages show friendly messages when something goes wrong.
Use +error.svelte in SvelteKit to create custom error pages.
Display the status and error.message to inform users clearly.
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
