0
0
Svelteframework~10 mins

Error pages (+error.svelte) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Error pages (+error.svelte)
User requests page
Page loads successfully?
NoError occurs
+error.svelte loads
Show page content
Display error message
User sees error page
When a page fails to load, SvelteKit shows the +error.svelte component to display a friendly error message.
Execution Sample
Svelte
<script>
  export let error;
  export let status;
</script>
<h1>{status}</h1>
<p>{error.message}</p>
This +error.svelte component shows the error status and message when a page fails.
Execution Table
StepActionInput/ErrorComponent StateOutput Rendered
1User requests /missing-pageNo page found (404)error.message='Not Found', status=404Render +error.svelte with 404 and 'Not Found'
2+error.svelte receives propserror.message='Not Found', status=404error and status set<h1>404</h1><p>Not Found</p>
3User sees error pageRendered HTMLStatic error page shownUser sees '404' and 'Not Found' message
4User requests /server-errorServer error (500)error.message='Internal Server Error', status=500Render +error.svelte with 500 and message
5+error.svelte receives propserror.message='Internal Server Error', status=500error and status set<h1>500</h1><p>Internal Server Error</p>
6User sees error pageRendered HTMLStatic error page shownUser sees '500' and 'Internal Server Error' message
7User requests /valid-pagePage loads OKNo errorPage content rendered, +error.svelte not used
8EndNo errorNo error stateNormal page shown, no error page
💡 Execution stops when page loads successfully or error page is shown for failure.
Variable Tracker
VariableStartAfter Step 1After Step 4After Step 7Final
error.messageundefined'Not Found''Internal Server Error'undefinedundefined
statusundefined404500undefinedundefined
Key Moments - 2 Insights
Why does +error.svelte show different messages?
Because it receives the error and status from the failed page load, as shown in execution_table rows 1 and 4.
When is +error.svelte NOT used?
When the page loads successfully without errors, like in execution_table row 7, the normal page content shows instead.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of status at step 2?
A404
B500
Cundefined
D200
💡 Hint
Check the 'Component State' column at step 2 in the execution_table.
At which step does the user see the 'Internal Server Error' message?
AStep 3
BStep 6
CStep 5
DStep 7
💡 Hint
Look for the 'Output Rendered' column mentioning 'Internal Server Error' in the execution_table.
If the page loads successfully, what happens to the error variables?
AThey keep their last error values
BThey show default error messages
CThey become undefined
DThey cause +error.svelte to render anyway
💡 Hint
See variable_tracker values after step 7 and final column.
Concept Snapshot
In SvelteKit, +error.svelte shows error pages.
It receives 'error' and 'status' props.
Displays error status code and message.
Used only when a page load fails.
Otherwise, normal page content shows.
Customize +error.svelte to style error pages.
Full Transcript
When a user requests a page, SvelteKit tries to load it. If the page loads successfully, the normal content shows. If there is an error, like a 404 or 500, SvelteKit uses the +error.svelte component. This component receives two pieces of information: the error object and the status code. It then displays these to the user, for example showing '404' and 'Not Found' for a missing page. The execution table shows step-by-step how the error page is rendered and what the user sees. Variables like error.message and status change only when an error occurs. When the page loads fine, these variables are undefined and +error.svelte is not used. This helps users understand what went wrong in a friendly way. You can customize +error.svelte to change how error pages look and what they say.