Skip to content

Commit

Permalink
Merge pull request #459 from kingtong/fix-custom-error-pickling
Browse files Browse the repository at this point in the history
Fix pickling exception with custom errors
  • Loading branch information
zachmoody authored Apr 18, 2022
2 parents 28a7db8 + 7f02b94 commit 0f1e588
Showing 1 changed file with 22 additions and 23 deletions.
45 changes: 22 additions & 23 deletions pynetbox/core/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,32 @@ class RequestError(Exception):
"""

def __init__(self, message):
req = message

def __init__(self, req):
if req.status_code == 404:
message = "The requested url: {} could not be found.".format(req.url)
self.message = "The requested url: {} could not be found.".format(req.url)
else:
try:
message = "The request failed with code {} {}: {}".format(
self.message = "The request failed with code {} {}: {}".format(
req.status_code, req.reason, req.json()
)
except ValueError:
message = (
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(RequestError, self).__init__(message)
super(RequestError, self).__init__(req)
self.req = req
self.request_body = req.request.body
self.base = req.url
self.error = req.text

def __str__(self):
return self.message


class AllocationError(Exception):
"""Allocation Exception
Expand All @@ -77,16 +78,15 @@ class AllocationError(Exception):
NetBox 3.1.1) or 409 Conflict (since NetBox 3.1.1+).
"""

def __init__(self, message):
req = message

message = "The requested allocation could not be fulfilled."

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

def __str__(self):
return self.error


class ContentError(Exception):
Expand All @@ -97,18 +97,17 @@ class ContentError(Exception):
exception is raised in those cases.
"""

def __init__(self, message):
req = message

message = (
"The server returned invalid (non-json) data. Maybe not " "a NetBox server?"
)

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

def __str__(self):
return self.error


class Request(object):
Expand Down

0 comments on commit 0f1e588

Please sign in to comment.