Bird
Raised Fist0
Rest APIprogramming~10 mins

Error codes for machine consumption in Rest API - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to return a JSON error response with status code 404.

Rest API
return jsonify({"error": "Resource not found"}), [1]
Drag options to blanks, or click blank then click option'
A200
B404
C500
D403
Attempts:
3 left
💡 Hint
Common Mistakes
Using 200 means success, not error.
Using 500 means server error, not resource missing.
2fill in blank
medium

Complete the code to include an error code in the JSON response for a bad request.

Rest API
return jsonify({"error": "Bad request", "code": [1]), 400
Drag options to blanks, or click blank then click option'
A"BAD_REQUEST"
B"400"
C"ERR400"
D"BAD_REQ"
Attempts:
3 left
💡 Hint
Common Mistakes
Using numeric strings instead of descriptive codes.
Using unclear or inconsistent codes.
3fill in blank
hard

Fix the error in the JSON error response to include a machine-readable error code and message.

Rest API
response = {"message": "Unauthorized access", "error_code": [1]
return jsonify(response), 401
Drag options to blanks, or click blank then click option'
A401
B"401"
CUnauthorized
D"UNAUTHORIZED"
Attempts:
3 left
💡 Hint
Common Mistakes
Using numeric codes without quotes.
Using unquoted words causing syntax errors.
4fill in blank
hard

Fill both blanks to create a JSON error response with a code and a human message.

Rest API
return jsonify({"error": [1], "message": [2]), 403
Drag options to blanks, or click blank then click option'
A"FORBIDDEN"
B"Access denied"
C"Not allowed"
D"403"
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping code and message values.
Using numeric codes as strings for messages.
5fill in blank
hard

Fill all three blanks to return a JSON error with code, message, and a numeric HTTP status.

Rest API
error_response = {"code": [1], "message": [2], "status": [3]
return jsonify(error_response), [3]
Drag options to blanks, or click blank then click option'
A"INVALID_INPUT"
B"Input data is invalid"
C422
D"422"
Attempts:
3 left
💡 Hint
Common Mistakes
Using status as a string instead of number.
Mixing up code and message values.

Practice

(1/5)
1.

What is the main purpose of using error codes in a REST API?

easy
A. To tell machines what happened during a request
B. To make the API run faster
C. To change the API's URL
D. To store user data securely

Solution

  1. Step 1: Understand the role of error codes

    Error codes are used to communicate the result of a request to the client or machine.
  2. Step 2: Identify the correct purpose

    The main purpose is to inform machines about success or failure of requests, enabling automated handling.
  3. Final Answer:

    To tell machines what happened during a request -> Option A
  4. Quick Check:

    Error codes = communicate request status [OK]
Hint: Error codes explain request results to machines [OK]
Common Mistakes:
  • Thinking error codes speed up the API
  • Confusing error codes with data storage
  • Believing error codes change URLs
2.

Which HTTP status code correctly indicates a successful request in a REST API?

Choose the correct code:

easy
A. 404
B. 500
C. 200
D. 301

Solution

  1. Step 1: Recall common HTTP status codes

    200 means OK (success), 404 means Not Found, 500 means Server Error, 301 means Redirect.
  2. Step 2: Identify success code

    200 is the standard code for a successful HTTP request.
  3. Final Answer:

    200 -> Option C
  4. Quick Check:

    Success code = 200 [OK]
Hint: 200 means success in HTTP status codes [OK]
Common Mistakes:
  • Choosing 404 thinking it means success
  • Confusing 500 with success
  • Selecting 301 which is a redirect
3.

Given this JSON error response from a REST API:

{
  "status": 404,
  "error": "Resource not found"
}

What does the 404 status code mean?

medium
A. The request was successful
B. The requested resource does not exist
C. The server encountered an error
D. The client is not authorized

Solution

  1. Step 1: Understand HTTP 404 status code meaning

    404 means the requested resource was not found on the server.
  2. Step 2: Match the error message with the code

    The message "Resource not found" confirms the meaning of 404.
  3. Final Answer:

    The requested resource does not exist -> Option B
  4. Quick Check:

    404 = resource missing [OK]
Hint: 404 means resource missing or not found [OK]
Common Mistakes:
  • Thinking 404 means success
  • Confusing 404 with server error
  • Assuming 404 means unauthorized
4.

Look at this REST API error response snippet:

{
  "status": 200,
  "error": "Invalid input data"
}

What is wrong with this error code usage?

medium
A. Status 200 should not be used with an error message
B. The error message is missing a code
C. Status 200 means server error
D. The JSON format is incorrect

Solution

  1. Step 1: Analyze the status code and message

    Status 200 means success, but the message says "Invalid input data" which is an error.
  2. Step 2: Identify the mismatch

    Using 200 with an error message is incorrect; error codes like 400 should be used for invalid input.
  3. Final Answer:

    Status 200 should not be used with an error message -> Option A
  4. Quick Check:

    Status 200 = success, not error [OK]
Hint: Match error messages with proper error status codes [OK]
Common Mistakes:
  • Ignoring mismatch between status and message
  • Thinking 200 can mean error
  • Assuming JSON format is wrong
5.

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:

hard
A. Return random status codes to confuse attackers
B. Return only HTTP status codes without any message body
C. Return success status codes even when errors happen, but include error details in HTML
D. Return HTTP status codes with clear JSON error messages explaining the problem

Solution

  1. Step 1: Understand best practices for machine-readable errors

    Machines need both status codes and clear JSON messages to understand errors and automate responses.
  2. 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.
  3. Final Answer:

    Return HTTP status codes with clear JSON error messages explaining the problem -> Option D
  4. Quick Check:

    Status + JSON error message = best practice [OK]
Hint: Use status codes plus JSON messages for clear machine errors [OK]
Common Mistakes:
  • Sending no message body with status codes
  • Using success codes for errors
  • Returning HTML instead of JSON for errors