Bird
Raised Fist0
Rest APIprogramming~10 mins

Error response format in Rest API - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Error response format
Client sends request
↓
Server processes request
↓
Error occurs?
No→Send success response
Yes↓
Create error response
↓
Send error response to client
The client sends a request, the server processes it, and if an error happens, the server creates and sends an error response back.
Execution Sample
Rest API
{
  "error": {
    "code": 404,
    "message": "Resource not found"
  }
}
This JSON shows a typical error response with a code and message.
Execution Table
StepActionConditionResultResponse Sent
1Client sends requestN/ARequest received by serverNo response yet
2Server processes requestN/AChecks if resource existsNo response yet
3Check resource existenceResource found?NoNo
4Create error responseError type: Not FoundPrepare JSON with code 404 and messageNo
5Send error responseN/AError JSON sent to client{"error":{"code":404,"message":"Resource not found"}}
6Client receives responseN/AClient sees error messageEnd
💡 Error response sent because resource was not found
Variable Tracker
VariableStartAfter Step 2After Step 4Final
requestemptyreceived with resource pathsamesame
resource_existsunknownchecked: falsefalsefalse
error_responsenonenone{"code":404,"message":"Resource not found"}{"code":404,"message":"Resource not found"}
Key Moments - 2 Insights
Why does the server send an error response instead of a success response?
Because the resource was not found (see execution_table step 3), the server creates and sends an error response (step 4 and 5).
What does the error response JSON contain?
It contains an error object with a code (404) and a message explaining the error (see execution_table step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the response sent at step 5?
A{"success":true}
B{"error":{"code":404,"message":"Resource not found"}}
CEmpty response
D{"error":{"code":500,"message":"Server error"}}
💡 Hint
Check the 'Response Sent' column at step 5 in the execution_table.
At which step does the server decide the resource does not exist?
AStep 1
BStep 5
CStep 3
DStep 2
💡 Hint
Look at the 'Condition' column in the execution_table to find when resource existence is checked.
If the resource was found, what would change in the execution table?
AStep 3 condition would be Yes and success response sent at step 5
BError response would still be sent
CNo response would be sent
DServer would crash
💡 Hint
Refer to the 'Condition' and 'Response Sent' columns in the execution_table for step 3 and 5.
Concept Snapshot
Error response format:
- Server sends JSON with error details
- Common fields: code (number), message (string)
- Sent when request cannot be fulfilled
- Helps client understand what went wrong
- Example: {"error":{"code":404,"message":"Resource not found"}}
Full Transcript
When a client sends a request to a server, the server processes it. If the requested resource is not found, the server creates an error response in JSON format. This response includes an error code like 404 and a message explaining the problem. The server then sends this error response back to the client. This helps the client know why the request failed. The error response format is a standard way to communicate problems in APIs.

Practice

(1/5)
1. What is the main purpose of an error response format in a REST API?
easy
A. To speed up the API response time
B. To clearly communicate what went wrong during a request
C. To encrypt the data sent to the client
D. To store user information securely

Solution

  1. Step 1: Understand the role of error responses

    Error responses tell the client what problem happened when processing their request.
  2. Step 2: Identify the main goal

    The main goal is to communicate the error clearly so the client can handle it properly.
  3. Final Answer:

    To clearly communicate what went wrong during a request -> Option B
  4. Quick Check:

    Error response purpose = clear communication [OK]
Hint: Error responses explain problems clearly to clients [OK]
Common Mistakes:
  • Thinking error responses speed up API
  • Confusing error format with encryption
  • Assuming error responses store data
2. Which of the following is a correct JSON structure for a simple error response in a REST API?
easy
A. {"error": {"code": 404, "message": "Not Found"}}
B. {"status": 200, "message": "Success"}
C. {"data": {"id": 1, "name": "Item"}}
D. {"error_code": "404", "error_msg": "Not Found"}

Solution

  1. Step 1: Identify standard error response keys

    Common keys include "error", "code", and "message" to describe the problem.
  2. Step 2: Check each option

    {"error": {"code": 404, "message": "Not Found"}} uses "error" with "code" and "message" correctly in JSON format.
  3. Final Answer:

    {"error": {"code": 404, "message": "Not Found"}} -> Option A
  4. Quick Check:

    Standard error keys = code and message [OK]
Hint: Look for 'error', 'code', and 'message' keys in JSON [OK]
Common Mistakes:
  • Confusing success response with error
  • Using wrong key names like error_code
  • Missing nested error object
3. Given this error response JSON:
{"error": {"code": 401, "message": "Unauthorized"}}

What HTTP status code should the API return?
medium
A. 401
B. 500
C. 404
D. 200

Solution

  1. Step 1: Match error code to HTTP status

    The error code 401 means "Unauthorized", which is an HTTP status code for authentication failure.
  2. Step 2: Confirm correct HTTP status

    The API should return HTTP status 401 to match the error code in the response body.
  3. Final Answer:

    401 -> Option A
  4. Quick Check:

    Error code 401 = HTTP status 401 [OK]
Hint: Match error code with HTTP status code [OK]
Common Mistakes:
  • Returning 200 for error responses
  • Confusing 401 with 404
  • Using 500 for client errors
4. You receive this error response from an API:
{"error": {"code": "403", "message": "Forbidden"}}

But your client expects the code as an integer. What is the best fix?
medium
A. Change the message to "Access Denied"
B. Remove the code field entirely
C. Change the code value to an integer 403 in the response
D. Wrap the code in quotes to make it a string

Solution

  1. Step 1: Identify data type mismatch

    The code is a string "403" but the client expects an integer 403.
  2. Step 2: Fix the data type in the response

    Change the code value from a string to an integer to match client expectations.
  3. Final Answer:

    Change the code value to an integer 403 in the response -> Option C
  4. Quick Check:

    Match data types for code field [OK]
Hint: Ensure error code data type matches client expectation [OK]
Common Mistakes:
  • Removing important error fields
  • Changing message instead of code type
  • Leaving code as string when integer needed
5. You want to design an error response format that includes an error code, a message, and optional details for debugging. Which JSON structure below best fits this need?
hard
A. {"error": "Bad Request", "code": 400}
B. {"status": "error", "code": 400, "msg": "Bad Request"}
C. {"error_code": 400, "error_message": "Bad Request"}
D. {"error": {"code": 400, "message": "Bad Request", "details": "Missing 'id' parameter"}}

Solution

  1. Step 1: Check required fields in each option

    We need code, message, and optional details inside a clear error object.
  2. 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".
  3. Final Answer:

    {"error": {"code": 400, "message": "Bad Request", "details": "Missing 'id' parameter"}} -> Option D
  4. Quick Check:

    Include code, message, and details in error object [OK]
Hint: Include optional details inside error object for clarity [OK]
Common Mistakes:
  • Omitting details field
  • Using inconsistent key names
  • Not nesting error info properly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard