Exceptions API
Exception hierarchy for error handling.
Exception Hierarchy
GDELTError (base)
├── APIError
│ ├── RateLimitError
│ ├── APIUnavailableError
│ └── InvalidQueryError
├── DataError
│ ├── ParseError
│ └── ValidationError
│ └── InvalidCodeError
├── ConfigurationError
├── BigQueryError
└── SecurityError
Base Exception
GDELTError
Bases: Exception
Base exception for all GDELT client errors.
All custom exceptions in this library inherit from this class, allowing consumers to catch all library-specific errors with a single handler.
API Exceptions
APIError
Bases: GDELTError
Base exception for all API-related errors.
Raised when errors occur during communication with GDELT REST APIs.
RateLimitError
Bases: APIError
Raised when API rate limits are exceeded.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Error description |
required |
retry_after
|
int | None
|
Optional number of seconds to wait before retrying. None if the retry duration is unknown. |
None
|
Source code in src/py_gdelt/exceptions.py
__str__()
Return string representation including retry information.
Source code in src/py_gdelt/exceptions.py
APIUnavailableError
Bases: APIError
Raised when a GDELT API is temporarily unavailable.
This typically indicates server-side issues or maintenance windows. Consider falling back to BigQuery when this occurs.
InvalidQueryError
Bases: APIError
Raised when API request parameters are invalid.
This indicates a client-side error in query construction or parameters.
Data Exceptions
DataError
Bases: GDELTError
Base exception for data processing and validation errors.
Raised when errors occur during data parsing, transformation, or validation.
ParseError
Bases: DataError, ValueError
Raised when data parsing fails.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Error description |
required |
raw_data
|
str | None
|
Optional raw data that failed to parse, for debugging purposes. |
None
|
Source code in src/py_gdelt/exceptions.py
__str__()
Return string representation with truncated raw data if available.
Source code in src/py_gdelt/exceptions.py
ValidationError
Bases: DataError
Raised when data validation fails.
This includes Pydantic validation errors and custom validation logic.
InvalidCodeError
Bases: ValidationError
Raised when an invalid GDELT code is encountered.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Error description |
required |
code
|
str
|
The invalid code value |
required |
code_type
|
str
|
Type of code (e.g., "cameo", "theme", "country", "fips") |
required |
Source code in src/py_gdelt/exceptions.py
__str__()
Other Exceptions
ConfigurationError
Bases: GDELTError
Raised when client configuration is invalid or incomplete.
This includes missing credentials, invalid settings, or misconfiguration.
BigQueryError
Bases: GDELTError
Raised when BigQuery operations fail.
This includes query execution errors, authentication failures, and quota/billing issues.
SecurityError
Bases: GDELTError
Raised when a security check fails.
This includes URL validation failures, path traversal detection, zip bomb detection, and other security-related issues.
Usage
import asyncio
import logging
from py_gdelt.exceptions import APIError, RateLimitError, DataError
logger = logging.getLogger(__name__)
try:
result = await client.doc.query(doc_filter)
except RateLimitError as e:
# Handle rate limiting with retry info
if e.retry_after:
await asyncio.sleep(e.retry_after)
except APIError as e:
# Handle other API errors (network, unavailable, etc.)
logger.error(f"API error: {e}")
except DataError as e:
# Handle data parsing errors
logger.error(f"Data error: {e}")
except Exception as e:
# Handle unexpected errors
logger.error(f"Unexpected error: {e}")