Skip to content

Commit c796318

Browse files
committed
When logging API error responses, parse any JSON payload
Don't log the raw JSON payload, parse it and print it in python format. If it can't be parsed, dump the string.
1 parent 056067a commit c796318

File tree

1 file changed

+9
-7
lines changed
  • python/neutron-understack/neutron_understack

1 file changed

+9
-7
lines changed

python/neutron-understack/neutron_understack/nautobot.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,18 @@ def make_api_request(
5656
except Exception as e:
5757
raise NautobotOSError(err=e) from e
5858

59+
if response.content:
60+
try:
61+
response_data = response.json()
62+
except requests.exceptions.JSONDecodeError:
63+
response_data = {"body": response.content}
64+
else:
65+
response_data = {"status_code": response.status_code}
66+
5967
if response.status_code >= 300:
6068
raise NautobotRequestError(
61-
code=response.status_code, url=full_url, body=response.content
69+
code=response.status_code, url=full_url, body=response_data
6270
)
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}
6971

7072
caller_function = inspect.stack()[1].function
7173
LOG.debug(

0 commit comments

Comments
 (0)