Overview
The Kelly AI Python SDK defines several error classes to help you handle different failure scenarios. All errors exceptInvalidApiKey inherit from the BaseError class.
Error classes
BaseError
The base exception class that all other errors inherit from (exceptInvalidApiKey).
success: Always set toFalseerror_message: The error message, either custom or the default message
TimeoutError
Raised when the API request exceeds the timeout limit. Message:"Internal Server Timeout, Please try again later"
When raised:
- When an
asyncio.TimeoutErroroccurs during the API request - Default timeout is 60 seconds for most methods
InvalidRequest
Raised when the API request is invalid or malformed. Message:"Invalid Request, Please read docs: https://api.princexd.tech/docs"
When raised:
- When the request parameters are invalid or missing required fields
- When the request format is incorrect
InvalidContent
Raised when the API returns content that cannot be processed. Message:"Invalid Content, Please report this: https://telegram.me/princexsupport"
When raised:
- When an
aiohttp.ContentTypeErroroccurs - When the response content type is unexpected or cannot be parsed as JSON
GenericApiError
Raised when a generic API error occurs that doesn’t fit other categories. Message:"Api Call Failed, Please report this: https://telegram.me/princexsupport"
When raised:
- When the API call fails for an unspecified reason
- Generic fallback for unexpected API errors
ConnectionError
Raised when the SDK cannot establish a connection to the API server. Message:"Failed to communicate server, Please report this: https://telegram.me/princexsupport"
When raised:
- When an
aiohttp.ClientConnectorErroroccurs - When the API returns a 502 status code
- Network connectivity issues
InvalidApiKey
Raised when the API key is invalid or unauthorized. When raised:- When the API returns a 401 (Unauthorized) status code
- When the API returns a 403 (Forbidden) status code
- When the API key is missing, invalid, or expired
BaseError. It directly inherits from Python’s built-in Exception class.
Usage:
Comprehensive error handling
Here’s an example of handling all possible errors in a single try-except block:Best practices
-
Always handle
InvalidApiKeyfirst: This error is most common when setting up the SDK -
Use specific exceptions: Catch specific exceptions before catching
BaseErrorto provide better error messages -
Log errors appropriately: Use the
error_messageattribute fromBaseErrorfor logging -
Implement retry logic: For
TimeoutErrorandConnectionError, consider implementing exponential backoff retry logic -
Report persistent issues: If you encounter
InvalidContentorGenericApiErrorfrequently, report them to support