Skip to content

nanosdk.errors

Exceptions raised by nanosdk clients.

Everything nanosdk raises (apart from pydantic's ValidationError for invalid call arguments, which happens before a request is sent) derives from NanoSDKError:

NanoSDKError
├── APIConnectionError        no response at all
│   └── APITimeoutError
├── APIStatusError            a 4xx/5xx response
└── ResponseValidationError   a 2xx response that doesn't match the declared type

NanoSDKError

Bases: Exception

Base class for every error nanosdk raises.

Catch it to handle any API failure in one place.

APIConnectionError

APIConnectionError(message: str, *, request: Request)

Bases: NanoSDKError

The request never got a response (DNS failure, refused connection, TLS, ...).

Raised after any retries allowed by the client's Retry policy are used up. The underlying httpx.TransportError is chained as __cause__.

Parameters:

Name Type Description Default
message str

Description of the failure.

required
request Request

The request that couldn't be sent.

required

Attributes:

Name Type Description
request

The request that couldn't be sent.

APITimeoutError

APITimeoutError(message: str, *, request: Request)

Bases: APIConnectionError

The request timed out (connect, read, write or pool timeout).

APIStatusError

APIStatusError(
    response: Response, *, body: Any, error: Any = None
)

Bases: NanoSDKError

The server answered with a 4xx or 5xx status.

Raised by Client._raise_for_status; override that to raise your own exceptions instead.

Example
try:
    api.get_user(user_id=1)
except nanosdk.APIStatusError as exc:
    if exc.status_code == 404:
        ...
    print(exc.error)  # parsed with error_model, if the client has one

Parameters:

Name Type Description Default
response Response

The error response.

required
body Any

The decoded response body.

required
error Any

body validated against the client's error_model, or None.

None

Attributes:

Name Type Description
response

The error response.

request Request

The request that produced it.

status_code int

The HTTP status code.

body

The response body as decoded JSON, or text if it wasn't JSON.

error

body validated against the client's error_model; None if the client has no error_model or the body didn't match it.

ResponseValidationError

ResponseValidationError(
    response: Response, error: ValidationError
)

Bases: NanoSDKError

A successful response's body didn't match the route's declared response type.

Parameters:

Name Type Description Default
response Response

The response whose body failed validation.

required
error ValidationError

The pydantic validation error.

required

Attributes:

Name Type Description
response

The response whose body failed validation.

error

The pydantic validation error, with details of every mismatch.