Skip to main content

Error Handling

Every error raised by the SDK inherits from dhivehigpt_sdk.DhivehiGPTError, which carries a message, a status_code (when the error came from an HTTP response), and the raw response payload.

from dhivehigpt_sdk import DhivehiGPT, DhivehiGPTError, NotFoundError

client = DhivehiGPT()

try:
client.voices.get("does-not-exist")
except NotFoundError as e:
print(e.status_code, e.message)
except DhivehiGPTError as e:
print("Something else went wrong:", e)

Exception reference

ExceptionHTTP statusCause
dhivehigpt_sdk.AuthenticationError401The X-Api-Key header is missing, invalid, or revoked.
dhivehigpt_sdk.PermissionDeniedError403E.g. the team does not have enough credits to generate audio.
dhivehigpt_sdk.NotFoundError404The requested resource does not exist.
dhivehigpt_sdk.ValidationError422The request body or parameters failed validation.
dhivehigpt_sdk.RateLimitError429More than 60 requests per minute were sent with this API key.
dhivehigpt_sdk.ServerError5xxAn upstream/server-side error occurred.
dhivehigpt_sdk.APIErrorother 4xxAny other non-2xx response.
dhivehigpt_sdk.APIConnectionErrorThe request could not reach the DhivehiGPT API at all.

Validation errors

ValidationError additionally exposes an errors dict with field-level messages, mirroring the API's errors object:

from dhivehigpt_sdk import ValidationError

try:
client.tasks.calculate(task="not-a-real-task", units=1000)
except ValidationError as e:
print(e.errors)
# {"task": ["The selected task is invalid."]}