0
0
Svelteframework~5 mins

Response helpers (json, error) in Svelte - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the json() helper do in SvelteKit?
The json() helper creates a response with JSON content and the correct Content-Type header. It formats data so the client can easily read it as JSON.
Click to reveal answer
beginner
How do you use the error() helper in SvelteKit?
You call error(status, message) to create an error response with a status code and message. This stops the request and shows an error page or message.
Click to reveal answer
intermediate
Why is it better to use json() instead of manually creating a JSON response?
Because json() automatically sets the right headers and formats the data correctly, making your code cleaner and less error-prone.
Click to reveal answer
intermediate
What happens if you use error(404, 'Not found') in a SvelteKit load function?
The request stops and SvelteKit shows a 404 error page with the message 'Not found'. This helps handle missing data or pages gracefully.
Click to reveal answer
beginner
Can you customize the error message shown to users when using error()?
Yes, you provide a custom message as the second argument to error(). This message can explain what went wrong in simple words.
Click to reveal answer
What header does the json() helper automatically set?
AContent-Type: text/html
BAuthorization: Bearer token
CContent-Type: application/json
DCache-Control: no-cache
What does error(500, 'Server error') do in SvelteKit?
AReturns a JSON response with status 200
BThrows a 500 error with message 'Server error'
CRedirects to the home page
DLogs the error but continues loading
Which helper should you use to send JSON data from a SvelteKit endpoint?
Afetch()
Berror()
Credirect()
Djson()
If you want to stop a request and show a 404 page, which helper do you use?
Aerror(404, 'Not found')
Bthrow new Error('Not found')
Credirect('/404')
Djson()
What type of value does json() expect as input?
AA JavaScript object or array
BA string of HTML
CA number only
DA boolean value
Explain how and why you would use the json() helper in a SvelteKit endpoint.
Think about sending data from server to client in a clean way.
You got /4 concepts.
    Describe the purpose of the error() helper and how it affects the user experience in SvelteKit.
    Consider what happens when something goes wrong on the server.
    You got /4 concepts.