Skip to content

Commit 23eff58

Browse files
committed
When logging raw payload of large API errors, truncate the payload
This is so we don't get pages of HTML in our container logs
1 parent 6885962 commit 23eff58

File tree

1 file changed

+10
-1
lines changed
  • python/neutron-understack/neutron_understack

1 file changed

+10
-1
lines changed

python/neutron-understack/neutron_understack/nautobot.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ class NautobotCustomFieldNotFoundError(exc.NeutronException):
2626
message = "Custom field with name %(cf_name)s not found for %(obj)s"
2727

2828

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+
2937
class Nautobot:
3038
"""Basic Nautobot wrapper because pynautobot doesn't expose plugin APIs."""
3139

@@ -60,7 +68,8 @@ def make_api_request(
6068
try:
6169
response_data = response.json()
6270
except requests.exceptions.JSONDecodeError:
63-
response_data = {"body": response.content}
71+
response_data = {"body": _truncated(response.content)}
72+
6473
else:
6574
response_data = {"status_code": response.status_code, "body": ""}
6675

0 commit comments

Comments
 (0)