Error handling

Every failure the SDK raises derives from ClientError. An unsuccessful HTTP response raises APIError or one of its subclasses. A network failure raises TransportError, and an unexpected response raises ProtocolError.

Exceptions​

ExceptionRaised when
AuthenticationErrorHTTP 401 or 403: the key or Management Key is wrong, or lacks permission
PaymentRequiredErrorHTTP 402: the balance is empty, or a lookup limit is reached
NotFoundErrorHTTP 404, such as an unknown address ID or licensee
RateLimitErrorHTTP 429, or 503 with API code 5030
ServerErrorAny other HTTP 5xx
APIErrorAny other unsuccessful HTTP response. Base class of the five above
TransportErrorThe request failed before a response arrived, such as a connection error or timeout
ProtocolErrorThe response was not a valid success envelope, had an unexpected success status or its result was the wrong JSON type, such as a list instead of an object

The SDK checks arguments against the API's schema before it sends a request. A wrong type or a missing settings key raises TypeError; a value out of range, too long or not matching its pattern raises ValueError. It does not check option values, so the API can add new ones.

Responses that do not match the spec​

If the API returns a result that breaks the response schema, such as a missing required field or a field of the wrong type, the SDK returns it rather than raising and emits a ResponseValidationWarning. The warning names the method and the field paths, never the values. Fields that match are typed as usual; a field that does not keeps the raw value, and a missing field raises AttributeError when read. Calling model_dump() on such a result can print Pydantic serializer warnings that include values, so pass warnings=False if you log it.

To fail instead, turn the warning into an error:

import warnings

from addresszen import ResponseValidationWarning

warnings.simplefilter("error", ResponseValidationWarning)

APIError attributes​

AttributeContents
statusHTTP status, such as 404
codeAPI error code, such as 4040. 0 when the body has none
messageAPI error message
bodyParsed JSON body as a dictionary
retry_afterRaw Retry-After header, or None
errors.pypython
from addresszen import AddressZen, APIError, AuthenticationError

with AddressZen("ak_test") as client:
    try:
        result = client.address_verify(query="1010 Cauthen Ln, Alamogordo, NM 88310")
    except AuthenticationError:
        print("Check the key, and that address verification is enabled on it")
    except APIError as error:
        print(error.status, error.code, error.message)

The exception's string holds the status, code and API message. The message and body can echo the address you sent, so treat them as personal data when you log them. The error codes guide lists each code and how to fix it.

Retries​

The SDK does not retry. A timed-out request may still have been processed and charged, so the SDK leaves retries to you. Retry RateLimitError, ServerError and TransportError with a bounded number of attempts. retry_after is either a number of seconds or an HTTP date.