Skip to content

Commit b29494c

Browse files
Include request method in low-level request error messages
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 4431a2b commit b29494c

3 files changed

Lines changed: 45 additions & 4 deletions

File tree

hyperbrowser/transport/async_transport.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,10 @@ async def _handle_response(self, response: httpx.Response) -> APIResponse:
6767
original_error=e,
6868
)
6969
except httpx.RequestError as e:
70-
request_url = str(e.request.url) if e.request else "unknown URL"
70+
request_method = e.request.method if e.request is not None else "UNKNOWN"
71+
request_url = str(e.request.url) if e.request is not None else "unknown URL"
7172
raise HyperbrowserError(
72-
f"Request failed for {request_url}", original_error=e
73+
f"Request {request_method} {request_url} failed", original_error=e
7374
)
7475

7576
async def post(

hyperbrowser/transport/sync.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ def _handle_response(self, response: httpx.Response) -> APIResponse:
5555
original_error=e,
5656
)
5757
except httpx.RequestError as e:
58-
request_url = str(e.request.url) if e.request else "unknown URL"
58+
request_method = e.request.method if e.request is not None else "UNKNOWN"
59+
request_url = str(e.request.url) if e.request is not None else "unknown URL"
5960
raise HyperbrowserError(
60-
f"Request failed for {request_url}", original_error=e
61+
f"Request {request_method} {request_url} failed", original_error=e
6162
)
6263

6364
def close(self) -> None:

tests/test_transport_response_handling.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ def _build_response(status_code: int, body: str) -> httpx.Response:
1313
return httpx.Response(status_code, request=request, text=body)
1414

1515

16+
class _RequestErrorResponse:
17+
def __init__(self, method: str, url: str) -> None:
18+
self._request = httpx.Request(method, url)
19+
20+
def raise_for_status(self) -> None:
21+
raise httpx.RequestError("network down", request=self._request)
22+
23+
1624
def test_sync_handle_response_with_non_json_success_body_returns_status_only():
1725
transport = SyncTransport(api_key="test-key")
1826
try:
@@ -26,6 +34,20 @@ def test_sync_handle_response_with_non_json_success_body_returns_status_only():
2634
transport.close()
2735

2836

37+
def test_sync_handle_response_with_request_error_includes_method_and_url():
38+
transport = SyncTransport(api_key="test-key")
39+
try:
40+
with pytest.raises(
41+
HyperbrowserError,
42+
match="Request GET https://example.com/network failed",
43+
):
44+
transport._handle_response(
45+
_RequestErrorResponse("GET", "https://example.com/network")
46+
)
47+
finally:
48+
transport.close()
49+
50+
2951
def test_async_handle_response_with_non_json_success_body_returns_status_only():
3052
async def run() -> None:
3153
transport = AsyncTransport(api_key="test-key")
@@ -42,6 +64,23 @@ async def run() -> None:
4264
asyncio.run(run())
4365

4466

67+
def test_async_handle_response_with_request_error_includes_method_and_url():
68+
async def run() -> None:
69+
transport = AsyncTransport(api_key="test-key")
70+
try:
71+
with pytest.raises(
72+
HyperbrowserError,
73+
match="Request POST https://example.com/network failed",
74+
):
75+
await transport._handle_response(
76+
_RequestErrorResponse("POST", "https://example.com/network")
77+
)
78+
finally:
79+
await transport.close()
80+
81+
asyncio.run(run())
82+
83+
4584
def test_sync_handle_response_with_error_and_non_json_body_raises_hyperbrowser_error():
4685
transport = SyncTransport(api_key="test-key")
4786
try:

0 commit comments

Comments
 (0)