Human-readable error messages help users understand what went wrong in simple words. They make fixing problems easier and improve user experience.
Human-readable error messages in Rest API
Start learning this pattern below
Jump into concepts and practice - no test required
{
"error": {
"code": "string",
"message": "string",
"details": "string (optional)"
}
}The code is a short identifier for the error.
The message is a clear, simple explanation for the user.
{
"error": {
"code": "INVALID_INPUT",
"message": "Your email address is not valid."
}
}{
"error": {
"code": "NOT_FOUND",
"message": "The requested item was not found.",
"details": "Item ID 123 does not exist."
}
}This simple API returns a user name by ID. If the user ID is missing, it sends a human-readable error message with a 404 status.
from flask import Flask, jsonify, request app = Flask(__name__) @app.route('/user/<int:user_id>') def get_user(user_id): users = {1: 'Alice', 2: 'Bob'} if user_id not in users: return jsonify({ "error": { "code": "NOT_FOUND", "message": f"User with ID {user_id} was not found." } }), 404 return jsonify({"user": users[user_id]}) if __name__ == '__main__': app.run(debug=True)
Keep messages short and clear, avoiding technical terms.
Use consistent error codes to help developers handle errors easily.
Include extra details only if they help the user fix the problem.
Human-readable error messages explain problems in simple words.
They improve user experience and help fix issues faster.
Use clear codes and messages in your API responses.
Practice
What is the main purpose of human-readable error messages in a REST API?
Solution
Step 1: Understand the role of error messages
Error messages should help users understand what went wrong.Step 2: Identify the best description
Human-readable means simple and clear, not technical or confusing.Final Answer:
To explain problems in simple words for users -> Option DQuick Check:
Human-readable = simple explanation [OK]
- Choosing technical jargon instead of simple words
- Assuming error messages should hide details
- Confusing human-readable with code-only messages
Which of the following is the correct JSON format for a human-readable error message in a REST API response?
{
"error": {
"code": 404,
"message": "Resource not found"
}
}Solution
Step 1: Check JSON syntax correctness
Valid JSON requires double quotes around keys and string values.Step 2: Match the expected structure
The error object should contain code and message keys with clear names.Final Answer:
{ "error": { "code": 404, "message": "Resource not found" } } -> Option BQuick Check:
Valid JSON with clear keys = { "error": { "code": 404, "message": "Resource not found" } } [OK]
- Using single quotes instead of double quotes in JSON
- Missing nested error object structure
- Using incorrect key names like 'msg' or 'error_code'
Given this API response code snippet, what will be the output message?
response = {
"status": 400,
"error": {
"code": "INVALID_INPUT",
"message": "Input value is not valid"
}
}
print(response["error"]["message"])Solution
Step 1: Access nested dictionary keys
response["error"] gives the nested dictionary, then ["message"] accesses the message string.Step 2: Print the message value
The value of response["error"]["message"] is "Input value is not valid".Final Answer:
Input value is not valid -> Option AQuick Check:
Nested key access = message string [OK]
- Printing the error code instead of message
- Confusing status code with error message
- Trying to access keys incorrectly causing KeyError
Identify the error in this REST API error response JSON and fix it for proper human-readable message format:
{
error: {
code: 401,
message: 'Unauthorized access'
}
}Solution
Step 1: Check JSON syntax rules
JSON keys and string values must be in double quotes, not single quotes or unquoted.Step 2: Fix the JSON format
Add double quotes around keys (error, code, message) and change single quotes to double quotes.Final Answer:
Replace single quotes with double quotes and add quotes around keys -> Option CQuick Check:
Valid JSON needs double quotes on keys and strings [OK]
- Using single quotes for strings in JSON
- Leaving keys unquoted causing syntax errors
- Changing numeric codes to strings unnecessarily
You want to design a REST API error response that helps users fix input errors quickly. Which approach is best?
{
"error": {
"code": "INVALID_EMAIL",
"message": "Email format is incorrect",
"field": "email",
"suggestion": "Use a valid email like user@example.com"
}
}Solution
Step 1: Identify helpful error response elements
Clear code, message, field, and suggestion guide users to fix errors.Step 2: Compare options for user-friendliness
Include error code, message, field name, and suggestion for fix provides detailed, human-readable info; others hide or confuse users.Final Answer:
Include error code, message, field name, and suggestion for fix -> Option AQuick Check:
Detailed, clear errors improve user experience [OK]
- Returning generic messages without guidance
- Using HTTP 200 for errors causing confusion
- Hiding error details from users
