Error Handling
Python SDK Error Handling
This page explains how the Demandbase Python SDK reports API errors, retries supported failures, and exposes error details to your application.
Error Types
The main SDK error type is demandbase.DemandBaseAPIError.
The SDK raises DemandBaseAPIError when:
- The Demandbase API returns an HTTP error response.
- The SDK prevents a request because required request values are missing.
- A response has an unexpected shape for the requested SDK method.
Network-level errors, such as DNS failures, connection failures, and request timeouts, can be raised by the underlying HTTP client. Catch those separately if your application needs custom network retry or alerting behavior.
Basic Handling Pattern
import demandbase
try:
with demandbase.DBClient(timeout=60.0, retry_count=2) as client:
response = client.b2b_api.list_subscriptions(page=1, per_page=10)
print(response.model_dump())
except demandbase.DemandBaseAPIError as error:
print(f"Demandbase API error: {error.http_status_code} - {error.error_message}")
except Exception:
# Handle network errors, timeouts, or unexpected application errors here.
raiseDemandBaseAPIError Attributes
DemandBaseAPIError Attributes| Attribute | Type | Description |
|---|---|---|
request_path | str | API path associated with the failed request. |
http_status_code | int | HTTP status code returned by the API or generated by the SDK. |
error_message | str | Error message returned by the API or generated by the SDK. |
request_headers | mapping or None | Request headers, when available. |
response_headers | mapping or None | Response headers, when available. Useful for rate-limit handling. |
payload | object or None | Request payload, when available. |
parameters | object or None | Query parameters, when available. |
Example:
except demandbase.DemandBaseAPIError as error:
print(error.request_path)
print(error.http_status_code)
print(error.error_message)
print(error.response_headers)Retries
DBClient accepts a retry_count argument.
import demandbase
with demandbase.DBClient(retry_count=2) as client:
response = client.b2b_api.list_subscriptions()The SDK retries these cases:
| Case | SDK behavior |
|---|---|
401 invalid or expired access token | Clears the cached token, requests a new token, and retries the request. |
429 rate-limit response | Retries up to retry_count. |
500, 502, 503, 504 server errors | Retries up to retry_count. |
For 429 and supported 5xx responses, retry backoff starts at 30 seconds and doubles up to a maximum of 600 seconds.
If retries are exhausted, the SDK raises DemandBaseAPIError.
Rate Limits
If a rate-limit response is returned after configured retries are exhausted, catch DemandBaseAPIError and inspect:
error.http_status_codeerror.response_headerserror.error_message
Example:
except demandbase.DemandBaseAPIError as error:
if error.http_status_code == 429:
print("Rate limit reached. Retry later.")
print(error.response_headers)
else:
raiseRate-limit header names can vary by API response. Use the returned headers to decide whether your application should retry later, queue the request, or reduce request volume.
Authentication Errors
The SDK reads credentials from environment variables:
DEMANDBASE_CLIENT_IDDEMANDBASE_CLIENT_SECRET
If either value is missing, client initialization raises RuntimeError.
If credentials are present but invalid, or the credentials do not have access to the requested API, the SDK raises DemandBaseAPIError.
Checklist:
- Confirm both environment variables are set in the runtime environment.
- Confirm the client ID and client secret are valid.
- Confirm the API Key Set includes the API permissions required by your integration.
- Confirm your integration is calling the production Demandbase API.
Validation Errors
The SDK validates some inputs before sending requests.
| Scenario | Exception |
|---|---|
| Page number is less than 1 | ValueError |
| Results-per-page value is less than 1 | ValueError |
| Local CSV file path cannot be read | ValueError |
CSV input is not a pandas.DataFrame, bytes, or path string | ValueError or TypeError, depending on the method |
| Required export job fields are missing | DemandBaseAPIError |
Example:
try:
with demandbase.DBClient() as client:
client.b2b_api.list_subscriptions(page=0)
except ValueError as error:
print(f"Invalid request value: {error}")Logging And Sensitive Data
SDK log output redacts bearer access tokens from authorization headers.
Request payloads, query parameters, and response bodies can still contain sensitive business data. Avoid logging full request or error context in production unless your application has appropriate access controls and retention policies.
Enable SDK logs when troubleshooting:
import demandbase
demandbase.enable_logging("INFO")Use DEBUG only during focused troubleshooting because it can produce verbose request diagnostics.
Troubleshooting Guide
| Symptom | What to check |
|---|---|
| Missing credential error | Confirm DEMANDBASE_CLIENT_ID and DEMANDBASE_CLIENT_SECRET are set before creating DBClient. |
401 persists after retry | Confirm the credentials are valid and have not been rotated or revoked. |
403 or permission-related errors | Confirm the API Key Set has permission for the endpoint or data type being requested. |
429 responses | Reduce request volume, increase spacing between requests, or retry later based on response headers. |
| Timeout errors | Confirm outbound network access and increase timeout, in seconds, for requests that normally take longer. |
| Unexpected response data | Confirm request filters, entity type, and account permissions match the data you expect to receive. |
Related Pages
Updated 1 day ago