0
0
Svelteframework~20 mins

Response helpers (json, error) in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
SvelteKit Response Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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!' });
}
AA 404 error response
BA plain text response with content 'Hello from SvelteKit!'
CAn HTML page with the text 'Hello from SvelteKit!'
D{"message":"Hello from SvelteKit!"}
Attempts:
2 left
💡 Hint
The json helper returns a JSON response with the given object as body.
state_output
intermediate
2: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');
}
A403 Forbidden
B500 Internal Server Error
C200 OK
D404 Not Found
Attempts:
2 left
💡 Hint
The error helper throws an HTTP error with the given status code.
📝 Syntax
advanced
2: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?
Areturn json({ success: true }, { header: { 'X-Custom': '123' } });
Breturn json({ success: true }, { headers: { 'X-Custom': '123' } });
Creturn json({ success: true }, { headers: { X-Custom: 123 } });
Dreturn json({ success: true }, { headers: 'X-Custom: 123' });
Attempts:
2 left
💡 Hint
The second argument to json is an options object with a headers property as an object.
🔧 Debug
advanced
2: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');
}
AThe status code must be a number, not a string, so '404' causes a runtime error.
BThe error helper must be called inside a try-catch block.
CThe error helper requires a third argument for the message.
DThe error helper only accepts status codes 400 and above, so 404 is invalid.
Attempts:
2 left
💡 Hint
Check the type of the first argument to error().
🧠 Conceptual
expert
3: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' });
}
AThe client receives a 500 Internal Server Error because of unreachable code.
BThe client receives a JSON response with { data: 'secret' } and status 200.
CThe client receives a 401 Unauthorized error response with the message.
DThe client receives no response and the request times out.
Attempts:
2 left
💡 Hint
Throwing error stops the function immediately, so code after throw does not run.