Error Handling and Troubleshooting
Eric @ CodableAI
Last Update há 2 anos
Encountering errors is a natural part of API integration. This guide helps you understand common error responses from the Document Summarization API, interpret error messages, and implement strategies to resolve issues effectively.
ContentsCommon Error Responses
The API may return various HTTP status codes to indicate the outcome of your requests. Understanding these codes helps in diagnosing and fixing issues.
400 Bad Request:
Indicates that the request was malformed or contained invalid parameters.
Example Response:
jsonCopy code{ "error": "Invalid 'tone' value. Allowed values are: energetic, neutral, calm, formal, informal.", "success": false, "completion_time": 500 }401 Unauthorized:
Occurs when the subscription key is missing, invalid, or expired.
Example Response:
jsonCopy code{ "error": "Invalid subscription key.", "success": false, "completion_time": 400 }403 Forbidden:
The request is understood, but it has been refused or access is not allowed.
Example Response:
jsonCopy code{ "error": "Access denied for the requested resource.", "success": false, "completion_time": 450 }404 Not Found:
The requested resource could not be found.
Example Response:
jsonCopy code{ "error": "The requested endpoint does not exist.", "success": false, "completion_time": 300 }429 Too Many Requests:
Indicates that you've exceeded the API's rate limits.
Example Response:
jsonCopy code{ "error": "Rate limit exceeded. Please try again later.", "success": false, "completion_time": 100 }500 Internal Server Error:
An unexpected error occurred on the server side.
Example Response:
jsonCopy code{ "error": "An error occurred while processing your request.", "success": false, "completion_time": 1500 }
Error Message Explanations
Invalid Parameters:
Errors like Invalid 'tone' value suggest that a parameter value does not match the allowed options. Review your request payload to ensure all parameters use valid values.
Authentication Errors:
Invalid subscription key or Access denied errors indicate issues with your API key. Ensure that you're using the correct key and that it hasn't expired or been revoked.
Rate Limiting:
Exceeding the number of allowed requests within a given timeframe triggers 429 Too Many Requests. Implement retries with exponential backoff to handle such scenarios gracefully.
Server Errors:
500 Internal Server Error points to issues on the API's end. In such cases, retrying the request after some time or contacting support may be necessary.
Debugging Failed Requests
Check Request Payload:
Ensure that your JSON payload is correctly formatted and includes all required parameters with valid values.
Verify Subscription Key:
Confirm that you're using the correct subscription key and that it is included in the request headers.
Inspect API Endpoint:
Make sure you're sending the request to the correct API endpoint and using the appropriate HTTP method.
Monitor Rate Limits:
Keep track of your API usage to avoid exceeding rate limits. Adjust your application's request frequency as needed.
Review API Documentation:
Refer to the API's official documentation for detailed information on parameters, responses, and error codes.
Best Practices for Robust Error Handling
Implement Retry Logic:
For transient errors like 500 Internal Server Error or 429 Too Many Requests, implement retries with exponential backoff to handle temporary issues.
Validate Inputs Before Sending:
Ensure all request parameters meet the API's requirements to minimize the likelihood of 400 Bad Request errors.
Use Logging:
Log all API requests and responses, including error details, to aid in debugging and monitoring.
Provide User Feedback:
Inform users gracefully when errors occur, and, if possible, provide actionable steps or alternative options.
Secure Error Information:
Avoid exposing sensitive information from error responses to end-users. Use generic messages and handle specifics within your application logic.
With effective error handling in place, continue to "Integrating the API with Popular Programming Languages" to streamline your development process across different environments.
