Skip to content

Commit 9e56369

Browse files
Support URL-like fallback objects in transport failure formatting
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 12110d5 commit 9e56369

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

hyperbrowser/transport/error_utils.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,16 @@ def _normalize_request_method(method: Any) -> str:
2222

2323

2424
def _normalize_request_url(url: Any) -> str:
25-
if not isinstance(url, str):
25+
if url is None:
2626
return "unknown URL"
27-
normalized_url = url.strip()
27+
raw_url = url
28+
if not isinstance(raw_url, str):
29+
try:
30+
raw_url = str(raw_url)
31+
except Exception:
32+
return "unknown URL"
33+
34+
normalized_url = raw_url.strip()
2835
if not normalized_url:
2936
return "unknown URL"
3037
if any(character.isspace() for character in normalized_url):

tests/test_transport_error_utils.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,16 @@ def test_format_request_failure_message_normalizes_non_string_fallback_values():
245245
assert message == "Request UNKNOWN unknown URL failed"
246246

247247

248+
def test_format_request_failure_message_supports_url_like_fallback_values():
249+
message = format_request_failure_message(
250+
httpx.RequestError("network down"),
251+
fallback_method="GET",
252+
fallback_url=httpx.URL("https://example.com/fallback"),
253+
)
254+
255+
assert message == "Request GET https://example.com/fallback failed"
256+
257+
248258
def test_format_generic_request_failure_message_normalizes_invalid_url_objects():
249259
message = format_generic_request_failure_message(
250260
method="GET",
@@ -254,6 +264,15 @@ def test_format_generic_request_failure_message_normalizes_invalid_url_objects()
254264
assert message == "Request GET unknown URL failed"
255265

256266

267+
def test_format_generic_request_failure_message_supports_url_like_values():
268+
message = format_generic_request_failure_message(
269+
method="GET",
270+
url=httpx.URL("https://example.com/path"),
271+
)
272+
273+
assert message == "Request GET https://example.com/path failed"
274+
275+
257276
def test_format_generic_request_failure_message_normalizes_invalid_method_values():
258277
message = format_generic_request_failure_message(
259278
method="GET /invalid",

tests/test_transport_response_handling.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,24 @@ def failing_get(*args, **kwargs):
439439
transport.close()
440440

441441

442+
def test_sync_transport_request_error_without_request_uses_url_like_fallback():
443+
transport = SyncTransport(api_key="test-key")
444+
original_get = transport.client.get
445+
446+
def failing_get(*args, **kwargs):
447+
raise httpx.RequestError("network down")
448+
449+
transport.client.get = failing_get # type: ignore[assignment]
450+
try:
451+
with pytest.raises(
452+
HyperbrowserError, match="Request GET https://example.com/fallback failed"
453+
):
454+
transport.get(httpx.URL("https://example.com/fallback")) # type: ignore[arg-type]
455+
finally:
456+
transport.client.get = original_get # type: ignore[assignment]
457+
transport.close()
458+
459+
442460
def test_sync_transport_request_error_without_request_uses_unknown_url_for_invalid_input():
443461
transport = SyncTransport(api_key="test-key")
444462
original_get = transport.client.get
@@ -477,6 +495,28 @@ async def failing_delete(*args, **kwargs):
477495
asyncio.run(run())
478496

479497

498+
def test_async_transport_request_error_without_request_uses_url_like_fallback():
499+
async def run() -> None:
500+
transport = AsyncTransport(api_key="test-key")
501+
original_delete = transport.client.delete
502+
503+
async def failing_delete(*args, **kwargs):
504+
raise httpx.RequestError("network down")
505+
506+
transport.client.delete = failing_delete # type: ignore[assignment]
507+
try:
508+
with pytest.raises(
509+
HyperbrowserError,
510+
match="Request DELETE https://example.com/fallback failed",
511+
):
512+
await transport.delete(httpx.URL("https://example.com/fallback")) # type: ignore[arg-type]
513+
finally:
514+
transport.client.delete = original_delete # type: ignore[assignment]
515+
await transport.close()
516+
517+
asyncio.run(run())
518+
519+
480520
def test_async_transport_request_error_without_request_uses_unknown_url_for_invalid_input():
481521
async def run() -> None:
482522
transport = AsyncTransport(api_key="test-key")

0 commit comments

Comments
 (0)