Skip to content

Commit 3216e16

Browse files
Support stringifiable request methods in transport diagnostics
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent eba041c commit 3216e16

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

hyperbrowser/transport/error_utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
from numbers import Real
23
import re
34
from typing import Any
45

@@ -29,11 +30,20 @@
2930

3031
def _normalize_request_method(method: Any) -> str:
3132
raw_method = method
33+
if isinstance(raw_method, bool):
34+
return "UNKNOWN"
35+
if isinstance(raw_method, Real):
36+
return "UNKNOWN"
3237
if isinstance(raw_method, (bytes, bytearray, memoryview)):
3338
try:
3439
raw_method = memoryview(raw_method).tobytes().decode("ascii")
3540
except (TypeError, ValueError, UnicodeDecodeError):
3641
return "UNKNOWN"
42+
elif not isinstance(raw_method, str):
43+
try:
44+
raw_method = str(raw_method)
45+
except Exception:
46+
return "UNKNOWN"
3747
if not isinstance(raw_method, str) or not raw_method.strip():
3848
return "UNKNOWN"
3949
normalized_method = raw_method.strip().upper()

tests/test_transport_error_utils.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ class _InvalidMethodTokenRequest:
4444
url = "https://example.com/invalid-method"
4545

4646

47+
class _MethodLikeRequest:
48+
class _MethodLike:
49+
def __str__(self) -> str:
50+
return "patch"
51+
52+
method = _MethodLike()
53+
url = "https://example.com/method-like"
54+
55+
4756
class _WhitespaceInsideUrlRequest:
4857
method = "GET"
4958
url = "https://example.com/with space"
@@ -101,6 +110,12 @@ def request(self): # type: ignore[override]
101110
return _InvalidMethodTokenRequest()
102111

103112

113+
class _RequestErrorWithMethodLikeContext(httpx.RequestError):
114+
@property
115+
def request(self): # type: ignore[override]
116+
return _MethodLikeRequest()
117+
118+
104119
class _RequestErrorWithWhitespaceInsideUrl(httpx.RequestError):
105120
@property
106121
def request(self): # type: ignore[override]
@@ -198,6 +213,15 @@ def test_extract_request_error_context_rejects_invalid_method_tokens():
198213
assert url == "https://example.com/invalid-method"
199214

200215

216+
def test_extract_request_error_context_accepts_stringifiable_method_values():
217+
method, url = extract_request_error_context(
218+
_RequestErrorWithMethodLikeContext("network down")
219+
)
220+
221+
assert method == "PATCH"
222+
assert url == "https://example.com/method-like"
223+
224+
201225
def test_extract_request_error_context_rejects_urls_with_whitespace():
202226
method, url = extract_request_error_context(
203227
_RequestErrorWithWhitespaceInsideUrl("network down")
@@ -501,6 +525,19 @@ def test_format_generic_request_failure_message_normalizes_non_string_method_val
501525
assert message == "Request UNKNOWN https://example.com/path failed"
502526

503527

528+
def test_format_generic_request_failure_message_supports_stringifiable_method_values():
529+
class _MethodLike:
530+
def __str__(self) -> str:
531+
return "delete"
532+
533+
message = format_generic_request_failure_message(
534+
method=_MethodLike(),
535+
url="https://example.com/path",
536+
)
537+
538+
assert message == "Request DELETE https://example.com/path failed"
539+
540+
504541
def test_format_generic_request_failure_message_supports_memoryview_method_values():
505542
message = format_generic_request_failure_message(
506543
method=memoryview(b"patch"),

0 commit comments

Comments
 (0)