When something goes wrong in a web service, we need to tell the user clearly what happened. An error response format is a simple way to share this information.
Error response format in Rest API
{
"error": {
"code": "string",
"message": "string",
"details": "string (optional)"
}
}The code is a short identifier for the error.
The message explains the error in simple words.
{
"error": {
"code": "404",
"message": "Resource not found"
}
}{
"error": {
"code": "401",
"message": "Unauthorized",
"details": "You need to log in to access this resource."
}
}This small web service returns an item by its ID. If the item is missing, it sends a clear error response with code 404 and a message.
from flask import Flask, jsonify, request app = Flask(__name__) @app.route('/data/<int:item_id>') def get_data(item_id): data_store = {1: 'apple', 2: 'banana'} if item_id not in data_store: return jsonify({ "error": { "code": "404", "message": "Item not found", "details": f"No item with id {item_id} exists." } }), 404 return jsonify({"item": data_store[item_id]}) if __name__ == '__main__': app.run(debug=True)
Always use clear and simple messages so users understand the problem.
Include HTTP status codes like 404 or 401 to help developers handle errors properly.
Optional details can give extra info but keep it short.
Error response format helps communicate problems clearly in web services.
It usually includes a code, a message, and sometimes extra details.
Using standard formats makes it easier for users and developers to understand errors.
Practice
Solution
Step 1: Understand the role of error responses
Error responses tell the client what problem happened when processing their request.Step 2: Identify the main goal
The main goal is to communicate the error clearly so the client can handle it properly.Final Answer:
To clearly communicate what went wrong during a request -> Option BQuick Check:
Error response purpose = clear communication [OK]
- Thinking error responses speed up API
- Confusing error format with encryption
- Assuming error responses store data
Solution
Step 1: Identify standard error response keys
Common keys include "error", "code", and "message" to describe the problem.Step 2: Check each option
{"error": {"code": 404, "message": "Not Found"}} uses "error" with "code" and "message" correctly in JSON format.Final Answer:
{"error": {"code": 404, "message": "Not Found"}} -> Option AQuick Check:
Standard error keys = code and message [OK]
- Confusing success response with error
- Using wrong key names like error_code
- Missing nested error object
{"error": {"code": 401, "message": "Unauthorized"}}What HTTP status code should the API return?
Solution
Step 1: Match error code to HTTP status
The error code 401 means "Unauthorized", which is an HTTP status code for authentication failure.Step 2: Confirm correct HTTP status
The API should return HTTP status 401 to match the error code in the response body.Final Answer:
401 -> Option AQuick Check:
Error code 401 = HTTP status 401 [OK]
- Returning 200 for error responses
- Confusing 401 with 404
- Using 500 for client errors
{"error": {"code": "403", "message": "Forbidden"}}But your client expects the code as an integer. What is the best fix?
Solution
Step 1: Identify data type mismatch
The code is a string "403" but the client expects an integer 403.Step 2: Fix the data type in the response
Change the code value from a string to an integer to match client expectations.Final Answer:
Change the code value to an integer 403 in the response -> Option CQuick Check:
Match data types for code field [OK]
- Removing important error fields
- Changing message instead of code type
- Leaving code as string when integer needed
Solution
Step 1: Check required fields in each option
We need code, message, and optional details inside a clear error object.Step 2: Evaluate each JSON structure
{"error": {"code": 400, "message": "Bad Request", "details": "Missing 'id' parameter"}} includes all three: code, message, and details nested inside "error".Final Answer:
{"error": {"code": 400, "message": "Bad Request", "details": "Missing 'id' parameter"}} -> Option DQuick Check:
Include code, message, and details in error object [OK]
- Omitting details field
- Using inconsistent key names
- Not nesting error info properly
Ready to prove it?
Pass the test and this pattern moves unknown
