Exceptions

Exception classes for error handling.

Base Exception

class django_api_orm.exceptions.APIException[source]

Bases: Exception

Base exception for all API-related errors.

Validation Exceptions

class django_api_orm.exceptions.ValidationException[source]

Bases: APIException

Raised when Pydantic validation fails.

Query Exceptions

class django_api_orm.exceptions.DoesNotExist[source]

Bases: APIException

Raised when a query returns no results (similar to Django’s DoesNotExist).

class django_api_orm.exceptions.MultipleObjectsReturned[source]

Bases: APIException

Raised when a query expected one result but returned multiple.

HTTP Exceptions

class django_api_orm.exceptions.HTTPStatusError[source]

Bases: APIException

Raised when an HTTP request returns an error status code.

This wraps httpx.HTTPStatusError to provide a consistent exception interface.

class django_api_orm.exceptions.AuthenticationError[source]

Bases: APIException

Raised when authentication fails (401 status).

class django_api_orm.exceptions.RateLimitError[source]

Bases: APIException

Raised when API rate limit is exceeded (429 status).

Connection Exceptions

class django_api_orm.exceptions.ConnectionError[source]

Bases: APIException

Raised when connection to the API fails.

class django_api_orm.exceptions.TimeoutError[source]

Bases: APIException

Raised when an API request times out.

Exception Hierarchy

APIException
├── ValidationException
├── DoesNotExist
├── MultipleObjectsReturned
├── ConnectionError
├── TimeoutError
├── AuthenticationError
├── RateLimitError
└── HTTPStatusError

Example Usage

Basic Exception Handling

from django_api_orm.exceptions import DoesNotExist, MultipleObjectsReturned

try:
    user = User.objects.get(id=1)
except DoesNotExist:
    print("User not found")
except MultipleObjectsReturned:
    print("Multiple users found")

HTTP Error Handling

from django_api_orm.exceptions import (
    AuthenticationError,
    RateLimitError,
    HTTPStatusError
)

try:
    user = User.objects.get(id=1)
except AuthenticationError:
    print("Please log in")
except RateLimitError:
    print("Rate limit exceeded")
except HTTPStatusError as e:
    print(f"HTTP error: {e}")

Connection Error Handling

from django_api_orm.exceptions import ConnectionError, TimeoutError

try:
    user = User.objects.get(id=1)
except ConnectionError:
    print("Failed to connect to API")
except TimeoutError:
    print("Request timed out")

Generic Error Handling

from django_api_orm.exceptions import APIException

try:
    user = User.objects.get(id=1)
except APIException as e:
    print(f"API error: {e}")

See Also

  • Exceptions for detailed exception handling documentation