Skip to content

Exceptions

Request and Response exceptions#

anac is based on httpx, so httpx exceptions are available here. See httpx._exceptions and https://www.python-httpx.org/exceptions/

Anac exception classes#

class anac.RequestDataError(message, method)

For invalid http request data

class anac.RequestParamsError(message)

For invalid http request params

The difference between data and parameters

anac.raise_for_status(response)

Raise the HTTPStatusError if one occurred.

anac.raise_for_status is a classic httpx.Response.raise_for_status() function, but with minor changes. As instance:

In [1]: from anac import api
   ...: 
   ...: a = api(
   ...:     "https://demo.netbox.dev",
   ...:     token="cf1dc7b04de5f27cfc93aba9e3f537d2ad6fdf8c",
   ...: )
   ...: # get openapi spec and create attributes/endpoints
   ...: # with python interpreter autocompletion
   ...: await a.openapi()

In [2]: test3_device = await a.dcim_device(
   ...:     post={
   ...:         "name": "test3",
   ...:         "device_role": {"name": 1},
   ...:         "site": {"name": "DM-Rochester"},
   ...:         "device_type": {"model": "C9200-48P"},
   ...:         "status": "planned",
   ...:     }
   ...: )

HTTPStatusError: Client error '400 Bad Request' (see https://httpstatuses.com/400) for url 'https://demo.netbox.dev/api/dcim/devices/' and 'POST' method.
Request parameters:
{"name": "test3", "device_role": {"name": 1}, "site": {"name": "DM-Rochester"}, "device_type": {"model": "C9200-48P"}, "status": "planned"}
Response:
{'device_role': ["Related object not found using the provided attributes: {'name': 1}"]}