Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Error Response Structure in REST API
📖 Scenario: You are building a REST API that needs to send clear error messages to clients when something goes wrong. This helps users understand what happened and how to fix it.
🎯 Goal: Create a simple error response structure in JSON format that includes an error code, a message, and details.
📋 What You'll Learn
Create a dictionary called error_response with keys code, message, and details
Set code to the integer 404
Set message to the string "Resource not found"
Set details to a list containing the string "The requested item does not exist"
Print the error_response dictionary as a JSON string
💡 Why This Matters
🌍 Real World
APIs need to send clear error messages so users and developers understand what went wrong and how to fix it.
💼 Career
Knowing how to structure and send error responses is important for backend developers and API designers.
Progress0 / 4 steps
1
Create the error response dictionary
Create a dictionary called error_response with these exact entries: code set to 404, message set to "Resource not found", and details set to a list containing "The requested item does not exist".
Rest API
Hint
Use curly braces {} to create a dictionary. The details value should be a list with one string inside.
2
Import the JSON module
Import the json module to convert the dictionary to a JSON string.
Rest API
Hint
Use import json at the top of your code.
3
Convert the dictionary to a JSON string
Use json.dumps() to convert the error_response dictionary to a JSON string and store it in a variable called json_error.
Rest API
Hint
Use json.dumps(error_response) to convert the dictionary.
4
Print the JSON error response
Print the variable json_error to display the JSON string of the error response.
Rest API
Hint
Use print(json_error) to show the JSON string.
Practice
(1/5)
1. What is the main purpose of an error response structure in a REST API?
easy
A. To clearly communicate what went wrong during a request
B. To speed up the API response time
C. To store user data securely
D. To format the successful response data
Solution
Step 1: Understand the role of error responses
Error responses are sent when something goes wrong to inform the user or client about the issue.
Step 2: Identify the purpose of the error structure
The structure helps communicate the problem clearly, usually with a code and message.
Final Answer:
To clearly communicate what went wrong during a request -> Option A
Quick Check:
Error response = clear problem message [OK]
Hint: Error responses explain problems clearly to users [OK]
Common Mistakes:
Thinking error responses speed up API
Confusing error response with data storage
Assuming error response formats success data
2. Which of the following is the correct JSON syntax for a simple error response with code 404 and message "Not Found"?
easy
A. {"error": {code: 404, message: "Not Found"}}
B. {"error": {"code": 404, "message": "Not Found"}}
C. {"error": {"code": "404", "message": Not Found}}
D. {"error": {"code": 404, message: Not Found}}
Solution
Step 1: Check JSON key and string syntax
Keys and string values must be in double quotes. Numbers like 404 do not need quotes.
Step 2: Identify the correct option
{"error": {"code": 404, "message": "Not Found"}} uses correct quotes for keys and strings, and number 404 without quotes.
Final Answer:
{"error": {"code": 404, "message": "Not Found"}} -> Option B
Quick Check:
JSON keys and strings need double quotes [OK]
Hint: Use double quotes for keys and string values in JSON [OK]
Why might this cause problems in your client code expecting code as a number?
medium
A. Because the error key is misspelled
B. Because the message is missing
C. Because the code is a string, not a number, causing type errors
D. Because the JSON is invalid
Solution
Step 1: Identify the data type of code field
The code field is given as a string "400" instead of a number 400.
Step 2: Understand client expectations
If client expects a number, receiving a string can cause type errors or failed comparisons.
Final Answer:
Because the code is a string, not a number, causing type errors -> Option C
Quick Check:
Type mismatch in code field causes errors [OK]
Hint: Check if code is number, not string, to avoid type errors [OK]
Common Mistakes:
Ignoring type differences
Assuming message is missing
Thinking JSON is invalid
5. You want to design an error response structure that includes an error code, a message, and optionally a list of field errors for validation issues. Which JSON structure below correctly supports this?
Step 1: Understand the need for multiple field errors
We want a list of objects, each with a field name and its error message.
Step 2: Check each option's fields format
{"error": {"code": 422, "message": "Validation failed", "fields": [{"field": "email", "error": "Invalid format"}]}} uses an array of objects with field and error keys, which is clear and extensible.
Step 3: Identify why others are incorrect
{"error": {"code": 422, "message": "Validation failed", "fields": "email: Invalid format"}} uses a string instead of structured data; {"error": {"code": 422, "message": "Validation failed", "fields": {"email": "Invalid format"}}} uses an object but not a list; {"error": {"code": 422, "message": "Validation failed", "fields": ["email", "Invalid format"]}} uses a list mixing field and message without keys.