Skip to content

Commit cace40f

Browse files
Normalize bytes-like method fallbacks in transport errors
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 59c214a commit cace40f

3 files changed

Lines changed: 38 additions & 3 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ Polling timeouts and repeated polling failures are surfaced as:
187187

188188
`HyperbrowserPollingError` also covers stalled pagination (no page-batch progress during result collection).
189189
Transport-level request failures include HTTP method + URL context in error messages.
190-
URL-like fallback objects are stringified for transport diagnostics, and bytes-like fallback URL values are UTF-8 decoded when valid; missing/malformed/sentinel URL inputs (for example `None`, booleans, invalid bytes, `null`/`undefined`/`nan`, or numeric-like values such as `123`/`1.5`/`1e6`) are normalized to `unknown URL`.
190+
URL-like fallback objects are stringified for transport diagnostics, bytes-like fallback methods/URLs are decoded when valid, and missing/malformed/sentinel URL inputs (for example `None`, booleans, invalid bytes, `null`/`undefined`/`nan`, or numeric-like values such as `123`/`1.5`/`1e6`) are normalized to `unknown URL`.
191191

192192
```python
193193
from hyperbrowser import Hyperbrowser

‎hyperbrowser/transport/error_utils.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,15 @@
2626

2727

2828
def _normalize_request_method(method: Any) -> str:
29-
if not isinstance(method, str) or not method.strip():
29+
raw_method = method
30+
if isinstance(raw_method, (bytes, bytearray, memoryview)):
31+
try:
32+
raw_method = memoryview(raw_method).tobytes().decode("ascii")
33+
except (TypeError, ValueError, UnicodeDecodeError):
34+
return "UNKNOWN"
35+
if not isinstance(raw_method, str) or not raw_method.strip():
3036
return "UNKNOWN"
31-
normalized_method = method.strip().upper()
37+
normalized_method = raw_method.strip().upper()
3238
if len(normalized_method) > _MAX_REQUEST_METHOD_LENGTH:
3339
return "UNKNOWN"
3440
if not _HTTP_METHOD_TOKEN_PATTERN.fullmatch(normalized_method):

‎tests/test_transport_error_utils.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,26 @@ def test_format_request_failure_message_normalizes_non_string_fallback_values():
246246
assert message == "Request UNKNOWN unknown URL failed"
247247

248248

249+
def test_format_request_failure_message_supports_ascii_bytes_method_values():
250+
message = format_request_failure_message(
251+
httpx.RequestError("network down"),
252+
fallback_method=b"post",
253+
fallback_url="https://example.com/fallback",
254+
)
255+
256+
assert message == "Request POST https://example.com/fallback failed"
257+
258+
259+
def test_format_request_failure_message_normalizes_invalid_bytes_method_values():
260+
message = format_request_failure_message(
261+
httpx.RequestError("network down"),
262+
fallback_method=b"\xff\xfe",
263+
fallback_url="https://example.com/fallback",
264+
)
265+
266+
assert message == "Request UNKNOWN https://example.com/fallback failed"
267+
268+
249269
def test_format_request_failure_message_normalizes_numeric_fallback_url_values():
250270
message = format_request_failure_message(
251271
httpx.RequestError("network down"),
@@ -437,6 +457,15 @@ def test_format_generic_request_failure_message_normalizes_non_string_method_val
437457
assert message == "Request UNKNOWN https://example.com/path failed"
438458

439459

460+
def test_format_generic_request_failure_message_supports_memoryview_method_values():
461+
message = format_generic_request_failure_message(
462+
method=memoryview(b"patch"),
463+
url="https://example.com/path",
464+
)
465+
466+
assert message == "Request PATCH https://example.com/path failed"
467+
468+
440469
def test_format_request_failure_message_truncates_very_long_fallback_urls():
441470
very_long_url = "https://example.com/" + ("a" * 1200)
442471
message = format_request_failure_message(

0 commit comments

Comments
 (0)