Challenge - 5 Problems
SvelteKit Response Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What does this SvelteKit endpoint return?
Consider this SvelteKit endpoint code. What is the JSON response when a GET request is made?
Svelte
import { json } from '@sveltejs/kit'; export function GET() { return json({ message: 'Hello from SvelteKit!' }); }
Attempts:
2 left
💡 Hint
The json helper returns a JSON response with the given object as body.
✗ Incorrect
The json helper from '@sveltejs/kit' creates a Response object with JSON content type and stringified body. So the client receives JSON with the message property.
❓ state_output
intermediate2:00remaining
What status code does this error helper send?
Given this SvelteKit endpoint, what HTTP status code will the client receive?
Svelte
import { error } from '@sveltejs/kit'; export function GET() { throw error(403, 'Access denied'); }
Attempts:
2 left
💡 Hint
The error helper throws an HTTP error with the given status code.
✗ Incorrect
Throwing error(403, 'Access denied') causes SvelteKit to respond with status 403 and the message.
📝 Syntax
advanced2:00remaining
Which option correctly uses the json helper with headers?
You want to return JSON with a custom header 'X-Custom: 123'. Which code snippet is correct?
Attempts:
2 left
💡 Hint
The second argument to json is an options object with a headers property as an object.
✗ Incorrect
Option B correctly passes headers as an object with string keys and values. Others have typos or wrong types.
🔧 Debug
advanced2:00remaining
Why does this endpoint cause a runtime error?
This SvelteKit endpoint throws an error but the client gets a 500 error instead of 404. Why?
Svelte
import { error } from '@sveltejs/kit'; export function GET() { throw error('404', 'Not found'); }
Attempts:
2 left
💡 Hint
Check the type of the first argument to error().
✗ Incorrect
The error helper expects a number status code. Passing a string like '404' causes a runtime type error.
🧠 Conceptual
expert3:00remaining
What happens if you return json() after throwing error() in the same endpoint?
Consider this code snippet in a SvelteKit endpoint. What is the actual response sent to the client?
Svelte
import { json, error } from '@sveltejs/kit'; export function GET() { throw error(401, 'Unauthorized'); return json({ data: 'secret' }); }
Attempts:
2 left
💡 Hint
Throwing error stops the function immediately, so code after throw does not run.
✗ Incorrect
The throw statement interrupts execution and sends the error response. The return json() is never reached.