MSDN Documentation

API Error Handling Guide

This guide outlines the standard error handling practices for our APIs, ensuring consistent and informative responses to client applications.

General Principles

When an error occurs, our APIs will return a JSON response containing detailed information about the problem. The response will include an HTTP status code that reflects the general nature of the error, and a JSON body with specific error details.

Error Response Format

All error responses adhere to the following JSON structure:

                
{
  "error": {
    "code": "INVALID_REQUEST",
    "message": "The request could not be processed due to invalid parameters.",
    "details": "The 'user_id' parameter must be a positive integer.",
    "field": "user_id",
    "status": 400
  }
}
                
            

Common HTTP Status Codes and Error Codes

400 Bad Request

Indicates that the request could not be understood or processed by the server due to malformed syntax or invalid parameters.

401 Unauthorized

Indicates that the request requires authentication credentials that were not provided or were invalid.

403 Forbidden

Indicates that the server understood the request but refuses to authorize it. The authenticated user does not have permission to perform the requested action.

404 Not Found

Indicates that the requested resource could not be found on the server.

429 Too Many Requests

Indicates that the user has sent too many requests in a given amount of time (rate limiting).

500 Internal Server Error

Indicates that the server encountered an unexpected condition that prevented it from fulfilling the request.

Example: Invalid Parameter

If a request to fetch user data is made with an invalid user ID format:

                    
GET /api/v1/users/abc

// Response (400 Bad Request)
{
  "error": {
    "code": "INVALID_PARAMETER_FORMAT",
    "message": "Invalid format for user ID.",
    "details": "The 'user_id' parameter must be a numeric identifier.",
    "field": "user_id",
    "status": 400
  }
}
                    
                

Example: Resource Not Found

If a request is made for a non-existent product:

                    
GET /api/v1/products/99999

// Response (404 Not Found)
{
  "error": {
    "code": "RESOURCE_NOT_FOUND",
    "message": "The requested product could not be found.",
    "details": "Product with ID '99999' does not exist.",
    "status": 404
  }
}
                    
                

Best Practices for API Consumers