0
0
Rest APIprogramming~10 mins

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

Choose your learning style9 modes available
Concept Flow - Error response format
Client sends request
Server processes request
Error occurs?
NoSend 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.