0
0
Rest APIprogramming~10 mins

Error codes for machine consumption in Rest API - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Error codes for machine consumption
Client sends request
Server processes request
Check for errors?
NoSend success response
Yes
Generate error code
Send error response with code
Client reads error code
Client handles error accordingly
The client sends a request, the server checks for errors, sends back an error code if needed, and the client uses that code to decide what to do.
Execution Sample
Rest API
GET /api/data
Response: 404 Not Found
{
  "error_code": "NOT_FOUND",
  "message": "Data not found"
}
A client requests data, server responds with a 404 error code and a machine-readable error code in JSON.
Execution Table
StepActionServer StatusResponse CodeResponse Body
1Client sends GET /api/dataProcessing requestN/AN/A
2Server checks data existenceData not foundN/AN/A
3Server generates error codeError generated404{"error_code": "NOT_FOUND", "message": "Data not found"}
4Server sends error responseResponse sent404{"error_code": "NOT_FOUND", "message": "Data not found"}
5Client receives responseResponse received404{"error_code": "NOT_FOUND", "message": "Data not found"}
6Client reads error_codeError handled404Client decides next steps
💡 Execution stops after client receives and processes the error response.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5Final
requestNoneGET /api/dataGET /api/dataGET /api/dataProcessed
server_statusIdleChecking dataError generatedResponse sentIdle
response_codeNoneNone404404404
response_bodyNoneNone{"error_code": "NOT_FOUND", "message": "Data not found"}{"error_code": "NOT_FOUND", "message": "Data not found"}{"error_code": "NOT_FOUND", "message": "Data not found"}
client_actionWaitingWaitingWaitingReceived errorHandled error
Key Moments - 2 Insights
Why does the server send both a numeric HTTP status code and a machine-readable error code?
The HTTP status code (like 404) tells the client the general error type, while the machine-readable error code (like "NOT_FOUND") gives a precise, consistent identifier for the error so the client can handle it programmatically. See execution_table rows 3 and 4.
What happens if the client ignores the error_code in the response body?
If the client ignores the error_code, it may not handle the error correctly or specifically. The client might only see the HTTP status but miss detailed info for proper action. See execution_table row 6 where client reads error_code to decide next steps.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the response_code at Step 3?
A404
B500
C200
D400
💡 Hint
Check the 'Response Code' column at Step 3 in the execution_table.
At which step does the client receive the error response?
AStep 4
BStep 5
CStep 2
DStep 6
💡 Hint
Look for when the client status changes to 'Response received' in the execution_table.
If the server sent a 200 status but included an error_code in the body, what would change in the execution_table?
AResponse Code would be 404 and error_code would be missing
BResponse Code and error_code would both be 200
CResponse Code would be 200 but error_code still present in Response Body
DNo change, response_code stays 404
💡 Hint
Think about how HTTP status and error_code are independent but usually consistent, see variable_tracker for response_code and response_body.
Concept Snapshot
Error codes for machine consumption:
- Server sends HTTP status code (e.g., 404) for general error type.
- Response body includes machine-readable error_code (e.g., "NOT_FOUND") for precise handling.
- Client reads both to decide next steps.
- This helps automated clients handle errors clearly and consistently.
Full Transcript
This visual trace shows how a client sends a request to a server, which checks for errors. If an error occurs, the server sends back an HTTP status code like 404 and a JSON body with a machine-readable error_code such as "NOT_FOUND". The client receives this response and reads the error_code to handle the error properly. The execution table tracks each step from request to client handling. Variables like response_code and response_body change as the server processes and responds. Key moments clarify why both codes are needed and what happens if the client ignores the error_code. Quiz questions test understanding of response codes and client-server interaction. This helps beginners see how error codes work for machines to understand and react to problems automatically.