Skip to content
intermediate Phase 60 · REST Implementation

REST Error Handling

Understanding Magento 2 REST error handling: HTTP status codes, error response format, and validation errors

45m
0 problems
Topic Progress 0%

HTTP Status Codes

Success Codes

Code Description
200 OK - Request successful
201 Created - Resource created
202 Accepted - Request accepted
204 No Content - Success, no response body

Client Error Codes

Code Description
400 Bad Request - Invalid request syntax
401 Unauthorized - Authentication required
403 Forbidden - Insufficient permissions
404 Not Found - Resource doesn't exist
405 Method Not Allowed - Wrong HTTP method
422 Unprocessable Entity - Validation errors
429 Too Many Requests - Rate limit exceeded

Server Error Codes

Code Description
500 Internal Server Error - Server error
502 Bad Gateway - Upstream server error
503 Service Unavailable - Service down

Usage Examples

# 200 OK
curl -X GET '/rest/V1/products/SKU-123'
# Response: 200 with product data

# 201 Created
curl -X POST '/rest/V1/products'
# Response: 201 with created product

# 401 Unauthorized
curl -X GET '/rest/V1/products'
# No token provided

# 404 Not Found
curl -X GET '/rest/V1/products/INVALID'
# Product doesn't exist

# 422 Validation Error
curl -X POST '/rest/V1/products'
# Invalid product data

Error Response Format

Standard Error Response

{
    "message": "Invalid product data.",
    "errors": [
        {
            "message": "The SKU is required.",
            "field": "sku",
            "code": 0
        },
        {
            "message": "The price must be greater than 0.",
            "field": "price",
            "code": 0
        }
    ],
    "parameters": {
        "fieldName": "sku"
    }
}

Error Response Fields

Field Description
message Main error message
errors Array of detailed errors
errors[].message Specific error message
errors[].field Related field name
errors[].code Error code
parameters Additional context

Authentication Error

{
    "message": "The consumer isn't authorized to access %resource.",
    "parameters": {
        "resource": "Magento_Catalog::products"
    }
}

Not Found Error

{
    "message": "No such entity with sku = INVALID-SKU"
}

Rate Limit Error

{
    "message": "Too many requests. Please try again later."
}

Validation Errors

Validation Error Response

{
    "message": "Invalid product data.",
    "errors": [
        {
            "message": "The SKU is required.",
            "field": "sku",
            "code": 0
        },
        {
            "message": "The price must be greater than 0.",
            "field": "price",
            "code": 0
        },
        {
            "message": "The product name is too long.",
            "field": "name",
            "code": 0
        }
    ]
}

Required Field Errors

{
    "message": "Invalid input data.",
    "errors": [
        {
            "message": "This is a required field.",
            "field": "sku",
            "code": 0
        },
        {
            "message": "This is a required field.",
            "field": "name",
            "code": 0
        }
    ]
}

Format Errors

{
    "message": "Invalid input data.",
    "errors": [
        {
            "message": "Please enter a valid email address.",
            "field": "email",
            "code": 0
        },
        {
            "message": "Please enter a valid phone number.",
            "field": "telephone",
            "code": 0
        }
    ]
}

Business Logic Errors

{
    "message": "The product is out of stock.",
    "errors": [
        {
            "message": "The requested qty is not available.",
            "field": "qty",
            "code": 0
        }
    ]
}

Debugging API Errors

Enable API Logging

// app/etc/di.xml
<config>
    <type name="Magento\Framework\Webapi\Rest\Request">
        <plugin name="api_request_logger" type="Vendor\Module\Plugin\RequestLoggerPlugin"/>
    </type>
</config>

Check Logs

# System logs
tail -f var/log/system.log

# Exception logs
tail -f var/log/exception.log

# API logs (if enabled)
tail -f var/log/api.log

Common Issues

Issue Solution
401 Unauthorized Check token validity
403 Forbidden Check ACL permissions
404 Not Found Verify endpoint URL
422 Validation Check request body
500 Server Error Check server logs

Debug Steps

  1. Check Request

    • URL is correct
    • HTTP method is correct
    • Headers are correct
    • Body is valid JSON
  2. Check Authentication

    • Token is valid
    • Token has required permissions
    • Token is not expired
  3. Check Response

    • HTTP status code
    • Error message
    • Error details
  4. Check Server

    • PHP error logs
    • Magento logs
    • Server error logs

Quiz

1. What HTTP status code indicates authentication required?

Question 1 options

2. What is the error response format?

Question 2 options

3. What status code indicates validation errors?

Question 3 options

Flashcards

Question

What does 401 status code mean?

Answer

Unauthorized - authentication required

Question

What does 404 status code mean?

Answer

Not Found - resource doesn't exist

Question

What does 422 status code mean?

Answer

Unprocessable Entity - validation errors

Question

What is the error response format?

Answer

JSON with message and errors array

Question

Where are API logs?

Answer

var/log/system.log and var/log/exception.log

Revision Notes

Key Takeaways

  • 1. HTTP status codes indicate success or failure
  • 2. Error responses use JSON with message and errors
  • 3. 401 = Unauthorized, 403 = Forbidden, 404 = Not Found
  • 4. 422 indicates validation errors
  • 5. Check logs for debugging API issues

Interview Tips

  • Know common HTTP status codes
  • Understand error response format
  • Be ready to debug API errors
  • Discuss error handling strategies

Cheat Sheet

Success:
  200 OK
  201 Created
  204 No Content

Client Errors:
  400 Bad Request
  401 Unauthorized
  403 Forbidden
  404 Not Found
  422 Validation Error

Server Errors:
  500 Internal Server Error

Error Format:
  { "message": "...", "errors": [...] }