netbox-community / pynetbox

Python API client library for Netbox.
Apache License 2.0
575 stars 170 forks source link

Provide More Details on 'pynetbox.lib.query.RequestError' #28

Closed bdlamprecht closed 6 years ago

bdlamprecht commented 6 years ago

I've been able to use the pynetbox library to develop some logic to begin to import data from all of the different sources of information that my company uses (i.e. Infoblox DNS/DHCP servers, billing databases, NetBrain, etc). Thanks for making this library which makes this process relatively easy.

One problem that I keep running into again and again is that when the request isn't formatted correctly or a required custom_field is not provided, the same error is returned no matter what: The request failed with code 400 Bad Request.

Not being a python expert, I'd like to know if is would be possible at all to reveal the specific details of the pynetbox.lib.query.RequestError similar to the way that the direct API within NetBox does, such as the following (the output below has been scrubbed):

{
  "non_field_errors": [
    "Duplicate prefix found in VRF red (100:100): 10.0.136.0/23"
  ]
}

It is entirely possible this functionality already exists, but I couldn't seem to find the documentation on how to access this information if that is the case.

Thanks again for this great library.

zachmoody commented 6 years ago

You can have a look the RequestError Exception and see what all it returns in https://github.com/digitalocean/pynetbox/blob/master/pynetbox/lib/query.py#L22-L39 - self.error is probably what you're looking for, but if you need more I'm also passing the entire requests object in self.req so you can have a look there too.

You'll probably just want to catch it like:

try:
  youquery
except pynetbox.RequestError as e:
  print(e.error)

That should work. Could be off on the import path; not where I can test it immediately.

Will leave this open so we can get some better docs on RequestError.

bdlamprecht commented 6 years ago

Got it, thanks for the clarification. Looking forward to the documentation on RequestError.