API Error Handling

This guide explains how to handle errors that may occur when using the CognisentAI API. Understanding and properly handling these errors is crucial for building robust applications.

Error Response Format

When an error occurs, the API will return a JSON response with an error object containing details about the error. The HTTP status code will also indicate the nature of the error.

{
  "error": {
    "code": "ERROR_CODE",
    "message": "A human-readable error message",
    "details": {
      // Additional error details if available
    }
  }
}

Common Error Codes

  • 400 Bad Request - The request was invalid or cannot be served
  • 401 Unauthorized - Authentication failed or user doesn't have permissions for requested operation
  • 403 Forbidden - The request is understood, but it has been refused or access is not allowed
  • 404 Not Found - The requested resource could not be found
  • 429 Too Many Requests - Rate limit has been exceeded
  • 500 Internal Server Error - Something went wrong on our end

Handling Errors

When building your application, make sure to:

  1. Check the HTTP status code of the response
  2. Parse the error message and code from the response body
  3. Implement appropriate error handling logic based on the error code
  4. Display user-friendly error messages to your users

Example Error Handling

try {
  const response = await fetch('https://api.cognisentai.com/v1/properties');
  if (!response.ok) {
    const errorData = await response.json();
    throw new Error(errorData.error.message);
  }
  const data = await response.json();
  // Process the data
} catch (error) {
  console.error('API Error:', error.message);
  // Handle the error appropriately in your application
}

Rate Limiting Errors

If you receive a 429 Too Many Requests error, you should implement exponential backoff and retry logic in your application. The response will include a Retry-After header indicating how long you should wait before retrying the request.

Need Help?

If you're experiencing persistent errors or need assistance with error handling, please contact our support team at api-support@cognisentai.com or refer to our detailed API documentation.