Error codes for machine consumption in Rest API - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When APIs return error codes for machines to read, it is important to know how the processing time changes as more errors or requests happen.
We want to understand how the time to handle error codes grows when the number of requests increases.
Analyze the time complexity of the following code snippet.
// Example: Handling error codes in API responses
function handleErrors(errors) {
for (let i = 0; i < errors.length; i++) {
logError(errors[i].code);
}
}
function logError(code) {
// Simulate logging error code
console.log(`Error code: ${code}`);
}
This code loops through a list of error codes and logs each one for machine consumption.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the error codes array.
- How many times: Once for each error in the list.
As the number of error codes increases, the time to process them grows in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 logging calls |
| 100 | 100 logging calls |
| 1000 | 1000 logging calls |
Pattern observation: Doubling the number of errors doubles the work.
Time Complexity: O(n)
This means the time to handle error codes grows directly with the number of errors.
[X] Wrong: "Logging error codes takes the same time no matter how many errors there are."
[OK] Correct: Each error code requires a separate action, so more errors mean more work.
Understanding how error handling scales helps you design APIs that respond efficiently as usage grows.
"What if the error codes were grouped and logged once per group instead of individually? How would the time complexity change?"
Practice
What is the main purpose of using error codes in a REST API?
Solution
Step 1: Understand the role of error codes
Error codes are used to communicate the result of a request to the client or machine.Step 2: Identify the correct purpose
The main purpose is to inform machines about success or failure of requests, enabling automated handling.Final Answer:
To tell machines what happened during a request -> Option AQuick Check:
Error codes = communicate request status [OK]
- Thinking error codes speed up the API
- Confusing error codes with data storage
- Believing error codes change URLs
Which HTTP status code correctly indicates a successful request in a REST API?
Choose the correct code:
Solution
Step 1: Recall common HTTP status codes
200 means OK (success), 404 means Not Found, 500 means Server Error, 301 means Redirect.Step 2: Identify success code
200 is the standard code for a successful HTTP request.Final Answer:
200 -> Option CQuick Check:
Success code = 200 [OK]
- Choosing 404 thinking it means success
- Confusing 500 with success
- Selecting 301 which is a redirect
Given this JSON error response from a REST API:
{
"status": 404,
"error": "Resource not found"
}What does the 404 status code mean?
Solution
Step 1: Understand HTTP 404 status code meaning
404 means the requested resource was not found on the server.Step 2: Match the error message with the code
The message "Resource not found" confirms the meaning of 404.Final Answer:
The requested resource does not exist -> Option BQuick Check:
404 = resource missing [OK]
- Thinking 404 means success
- Confusing 404 with server error
- Assuming 404 means unauthorized
Look at this REST API error response snippet:
{
"status": 200,
"error": "Invalid input data"
}What is wrong with this error code usage?
Solution
Step 1: Analyze the status code and message
Status 200 means success, but the message says "Invalid input data" which is an error.Step 2: Identify the mismatch
Using 200 with an error message is incorrect; error codes like 400 should be used for invalid input.Final Answer:
Status 200 should not be used with an error message -> Option AQuick Check:
Status 200 = success, not error [OK]
- Ignoring mismatch between status and message
- Thinking 200 can mean error
- Assuming JSON format is wrong
You want to design a REST API that returns error codes machines can easily understand and act on. Which approach is best?
Choose the best practice:
Solution
Step 1: Understand best practices for machine-readable errors
Machines need both status codes and clear JSON messages to understand errors and automate responses.Step 2: Evaluate options
Return HTTP status codes with clear JSON error messages explaining the problem provides both HTTP status codes and JSON messages, which is the recommended approach.Final Answer:
Return HTTP status codes with clear JSON error messages explaining the problem -> Option DQuick Check:
Status + JSON error message = best practice [OK]
- Sending no message body with status codes
- Using success codes for errors
- Returning HTML instead of JSON for errors
