Skip to content

Exceptions

pynetbox raises a small set of dedicated exceptions in response to error conditions. They all live in pynetbox.core.query and are re-exported at the top level of the pynetbox package, so they can also be imported as pynetbox.RequestError, pynetbox.ContentError, pynetbox.AllocationError, and pynetbox.ParameterValidationError.

RequestError

pynetbox.core.query.RequestError

Bases: Exception

Basic Request Exception.

More detailed exception that returns the original requests object for inspection. Along with some attributes with specific details from the requests object. If return is json we decode and add it to the message.

Examples
try:
    nb.dcim.devices.create(name="destined-for-failure")
except pynetbox.RequestError as e:
    print(e.error)
Source code in pynetbox/core/query.py
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
class RequestError(Exception):
    """Basic Request Exception.

    More detailed exception that returns the original requests object
    for inspection. Along with some attributes with specific details
    from the requests object. If return is json we decode and add it
    to the message.

    ## Examples

    ```python
    try:
        nb.dcim.devices.create(name="destined-for-failure")
    except pynetbox.RequestError as e:
        print(e.error)
    ```
    """

    def __init__(self, req):
        if req.status_code == 404:
            self.message = "The requested url: {} could not be found.".format(req.url)
        else:
            try:
                self.message = "The request failed with code {} {}: {}".format(
                    req.status_code, req.reason, req.json()
                )
            except ValueError:
                self.message = (
                    "The request failed with code {} {} but more specific "
                    "details were not returned in json. Check the NetBox Logs "
                    "or investigate this exception's error attribute.".format(
                        req.status_code, req.reason
                    )
                )

        super().__init__(self.message)
        self.req = req
        self.request_body = req.request.body
        self.base = req.url
        self.error = req.text

    def __str__(self):
        return self.message

ContentError

pynetbox.core.query.ContentError

Bases: Exception

Content Exception.

If the API URL does not point to a valid NetBox API, the server may return a valid response code, but the content is not json. This exception is raised in those cases.

Source code in pynetbox/core/query.py
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
class ContentError(Exception):
    """Content Exception.

    If the API URL does not point to a valid NetBox API, the server may
    return a valid response code, but the content is not json. This
    exception is raised in those cases.
    """

    def __init__(self, req):
        super().__init__(req)
        self.req = req
        self.request_body = req.request.body
        self.base = req.url
        self.error = (
            "The server returned invalid (non-json) data. Maybe not a NetBox server?"
        )

    def __str__(self):
        return self.error

AllocationError

pynetbox.core.query.AllocationError

Bases: Exception

Allocation Exception.

Used with available-ips/available-prefixes when there is no room for allocation and NetBox returns 409 Conflict.

Source code in pynetbox/core/query.py
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
class AllocationError(Exception):
    """Allocation Exception.

    Used with available-ips/available-prefixes when there is no
    room for allocation and NetBox returns 409 Conflict.
    """

    def __init__(self, req):
        super().__init__(req)
        self.req = req
        self.request_body = req.request.body
        self.base = req.url
        self.error = "The requested allocation could not be fulfilled."

    def __str__(self):
        return self.error

ParameterValidationError

pynetbox.core.query.ParameterValidationError

Bases: Exception

API parameter validation Exception.

Raised when filter parameters do not match Netbox OpenAPI specification.

Examples
try:
    nb.dcim.devices.filter(field_which_does_not_exist="destined-for-failure")
except pynetbox.ParameterValidationError as e:
    print(e.error)
Source code in pynetbox/core/query.py
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
class ParameterValidationError(Exception):
    """API parameter validation Exception.

    Raised when filter parameters do not match Netbox OpenAPI specification.

    ## Examples

    ```python
    try:
        nb.dcim.devices.filter(field_which_does_not_exist="destined-for-failure")
    except pynetbox.ParameterValidationError as e:
        print(e.error)
    ```
    """

    def __init__(self, errors):
        super().__init__(errors)
        self.error = f"The request parameter validation returned an error: {errors}"

    def __str__(self):
        return self.error

Example

import pynetbox

nb = pynetbox.api('http://localhost:8000', token='your-token')

try:
    nb.dcim.devices.create(name='destined-for-failure')
except pynetbox.RequestError as e:
    # The error returned by the server is exposed as e.error.
    print(e.error)