Skip to content

Commit 4fdd0bf

Browse files
Truncate extremely long request URLs in error messages
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 8fc8342 commit 4fdd0bf

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

hyperbrowser/transport/error_utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
_HTTP_METHOD_TOKEN_PATTERN = re.compile(r"^[!#$%&'*+\-.^_`|~0-9A-Z]+$")
88
_MAX_ERROR_MESSAGE_LENGTH = 2000
9+
_MAX_REQUEST_URL_DISPLAY_LENGTH = 1000
910

1011

1112
def _normalize_request_method(method: Any) -> str:
@@ -29,6 +30,8 @@ def _normalize_request_url(url: Any) -> str:
2930
ord(character) < 32 or ord(character) == 127 for character in normalized_url
3031
):
3132
return "unknown URL"
33+
if len(normalized_url) > _MAX_REQUEST_URL_DISPLAY_LENGTH:
34+
return f"{normalized_url[:_MAX_REQUEST_URL_DISPLAY_LENGTH]}... (truncated)"
3235
return normalized_url
3336

3437

tests/test_transport_error_utils.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,18 @@ def test_format_request_failure_message_normalizes_lowercase_fallback_method():
204204
assert message == "Request POST https://example.com/fallback failed"
205205

206206

207+
def test_format_request_failure_message_truncates_very_long_fallback_urls():
208+
very_long_url = "https://example.com/" + ("a" * 1200)
209+
message = format_request_failure_message(
210+
httpx.RequestError("network down"),
211+
fallback_method="GET",
212+
fallback_url=very_long_url,
213+
)
214+
215+
assert "Request GET https://example.com/" in message
216+
assert "... (truncated) failed" in message
217+
218+
207219
def test_extract_error_message_handles_recursive_list_payloads():
208220
recursive_payload = []
209221
recursive_payload.append(recursive_payload)

0 commit comments

Comments
 (0)