11
11
12
12
13
13
class NautobotRequestError (exc .NeutronException ):
14
- message = "Nautobot API returned error %(code)s for %(url)s: %(body)s"
14
+ message = "Nautobot API ERROR %(code)s for %(url)s %(method)s %(payload )s: %(body)s"
15
15
16
16
17
17
class NautobotOSError (exc .NeutronException ):
@@ -26,6 +26,14 @@ class NautobotCustomFieldNotFoundError(exc.NeutronException):
26
26
message = "Custom field with name %(cf_name)s not found for %(obj)s"
27
27
28
28
29
+ def _truncated (message : str | bytes , maxlen = 200 ) -> str :
30
+ input = str (message )
31
+ if len (input ) <= maxlen :
32
+ return input
33
+
34
+ return f"{ input [:maxlen ]} ...{ len (input ) - maxlen } more chars"
35
+
36
+
29
37
class Nautobot :
30
38
"""Basic Nautobot wrapper because pynautobot doesn't expose plugin APIs."""
31
39
@@ -56,16 +64,25 @@ def make_api_request(
56
64
except Exception as e :
57
65
raise NautobotOSError (err = e ) from e
58
66
67
+ if response .content :
68
+ try :
69
+ response_data = response .json ()
70
+ except requests .exceptions .JSONDecodeError :
71
+ response_data = {"body" : _truncated (response .content )}
72
+
73
+ else :
74
+ response_data = {"status_code" : response .status_code , "body" : "" }
75
+
59
76
if response .status_code >= 300 :
77
+ response_data = response_data .get ("error" , response_data )
78
+
60
79
raise NautobotRequestError (
61
- code = response .status_code , url = full_url , body = response .content
80
+ code = response .status_code ,
81
+ url = full_url ,
82
+ method = method ,
83
+ payload = payload ,
84
+ body = response_data ,
62
85
)
63
- if not response .content :
64
- response_data = {"status_code" : response .status_code }
65
- try :
66
- response_data = response .json ()
67
- except requests .exceptions .JSONDecodeError :
68
- response_data = {"body" : response .content }
69
86
70
87
caller_function = inspect .stack ()[1 ].function
71
88
LOG .debug (
0 commit comments