MSDN Documentation

Microsoft Developer Network

API Design: Error Handling

Effective error handling is crucial for building robust, user-friendly, and maintainable APIs. Clear and consistent error messages help developers integrate with your API smoothly and diagnose issues efficiently.

Core Principles

Standard Error Response Format

We recommend a JSON-based error response format that includes the following fields:

JSON Error Response Example


{
  "error": {
    "code": "INVALID_REQUEST_PARAMETER",
    "message": "The 'userId' parameter is missing or invalid.",
    "details": "Please provide a valid UUID for the user identifier.",
    "target": "userId",
    "requestId": "a1b2c3d4-e5f6-7890-1234-567890abcdef",
    "timestamp": "2023-10-27T10:30:00Z"
  }
}
                

Field Definitions:

HTTP Status Codes

Use standard HTTP status codes to indicate the category of the error. Here are some common ones:

Always strive to return the most specific 4xx status code applicable.

Common Error Scenarios and Examples

1. Invalid Input

Occurs when a request parameter is missing, has an incorrect format, or an invalid value.

Request:

POST /users
Headers: Content-Type: application/json
Body: {"name": "John Doe", "email": "invalid-email"}

Response (HTTP 400 Bad Request):


{
  "error": {
    "code": "INVALID_EMAIL_FORMAT",
    "message": "The provided email address is not valid.",
    "details": "Please ensure the email follows standard format (e.g., user@example.com).",
    "target": "email",
    "requestId": "xyz789abc-..."
  }
}
                

2. Resource Not Found

Occurs when the requested entity does not exist.

Request:

GET /products/99999

Response (HTTP 404 Not Found):


{
  "error": {
    "code": "RESOURCE_NOT_FOUND",
    "message": "The product with ID '99999' was not found.",
    "target": "/products/99999",
    "requestId": "pqr123stu-..."
  }
}
                

3. Authentication/Authorization Issues

For authentication errors (e.g., missing API key), use 401 Unauthorized. For authorization errors (e.g., user doesn't have permission), use 403 Forbidden.

Response (HTTP 401 Unauthorized):


{
  "error": {
    "code": "AUTHENTICATION_FAILED",
    "message": "API key is missing or invalid.",
    "details": "Please include a valid 'X-API-Key' header.",
    "requestId": "uvw456xyz-..."
  }
}
                

Response (HTTP 403 Forbidden):


{
  "error": {
    "code": "PERMISSION_DENIED",
    "message": "You do not have permission to access this resource.",
    "details": "Your current role does not allow this operation.",
    "requestId": "ghi789jkl-..."
  }
}
                

Best Practices