Passing status as a separate argument without object
Using error() to send success responses
Using Response constructor incorrectly
3. Consider this SvelteKit endpoint code:
import { json, error } from '@sveltejs/kit';
export function GET() {
const data = { user: 'Alice' };
if (!data.user) {
throw error(404, 'User not found');
}
return json(data);
}
What will be the HTTP status code and response if data.user is missing?
medium
A. Status 200 with empty JSON object
B. Status 404 with message 'User not found'
C. Status 500 with server error
D. Status 200 with user data
Solution
Step 1: Analyze the condition
If data.user is missing (falsy), the code throws error(404, 'User not found').
Step 2: Understand error() behavior
The error() helper throws an HTTP error with the given status and message.
Final Answer:
Status 404 with message 'User not found' -> Option B
Quick Check:
Missing user triggers error(404) [OK]
Hint: error() throws HTTP errors with status and message [OK]
Common Mistakes:
Assuming json() runs even if error is thrown
Thinking error() returns JSON data
Confusing status codes for errors
4. Identify the error in this SvelteKit endpoint code snippet:
import { json, error } from '@sveltejs/kit';
export function POST() {
const data = { success: true };
return error(200, 'All good');
}
medium
A. Using error() with status 200 is incorrect
B. Missing return type annotation
C. POST function must be async
D. json() should be used instead of error() to send success
Solution
Step 1: Check status code validity
The error() helper requires status codes 400-599. Using 200 triggers "Non-error status code 200".
Step 2: Correct helper for success
For success (status 200), use json() instead of error().
Final Answer:
Using error() with status 200 is incorrect -> Option A
Quick Check:
Use json() for success, error() for errors [OK]
Hint: error() is for errors, not success responses [OK]
Common Mistakes:
Using error() with 200 status
Not returning json() for success
Assuming error() can send normal data
5. You want to create a SvelteKit endpoint that returns a JSON list of items but throws a 400 error if the list is empty. Which code snippet correctly implements this behavior?
hard
A. export function GET() {
const items = [];
if (items.length === 0) {
throw error(400, 'No items found');
}
return json(items);
}
B. export function GET() {
const items = [];
if (!items) {
return error(400, 'No items found');
}
return json(items);
}
C. export function GET() {
const items = [];
if (items.length === 0) {
return json({ error: 'No items found' }, { status: 400 });
}
return json(items);
}
D. export function GET() {
const items = [];
if (items.length === 0) {
throw json({ error: 'No items found' }, { status: 400 });
}
return json(items);
}
Solution
Step 1: Check error throwing for empty list
export function GET() {
const items = [];
if (items.length === 0) {
throw error(400, 'No items found');
}
return json(items);
} correctly throws error(400, 'No items found') when the list is empty.
Step 2: Validate response for non-empty list
It returns json(items) properly when items exist.
Final Answer:
export function GET() {
const items = [];
if (items.length === 0) {
throw error(400, 'No items found');
}
return json(items);
} -> Option A
Quick Check:
Throw error() for HTTP errors, json() for data [OK]
Hint: Throw error() for bad status, json() for good data [OK]
Common Mistakes:
Returning error() instead of throwing it
Using json() with error status instead of throwing error()
Throwing json() which is invalid
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.