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 | |
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 | |
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 | |
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 | |
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)