Skip to content

Commit 47e9b56

Browse files
Coerce non-string transport error payload messages
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent a1ccc26 commit 47e9b56

2 files changed

Lines changed: 30 additions & 7 deletions

File tree

hyperbrowser/transport/error_utils.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@ def extract_error_message(response: httpx.Response, fallback_error: Exception) -
1010
return response.text or str(fallback_error)
1111

1212
if isinstance(error_data, dict):
13-
return (
14-
error_data.get("message")
15-
or error_data.get("error")
16-
or response.text
17-
or str(fallback_error)
18-
)
13+
message = error_data.get("message") or error_data.get("error")
14+
if message is not None:
15+
return str(message)
16+
return response.text or str(fallback_error)
1917
if isinstance(error_data, str):
2018
return error_data
21-
return response.text or str(fallback_error)
19+
return str(response.text or str(fallback_error))

tests/test_transport_response_handling.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,28 @@ async def run() -> None:
9090
await transport.close()
9191

9292
asyncio.run(run())
93+
94+
95+
def test_sync_handle_response_with_non_string_message_field_coerces_to_string():
96+
transport = SyncTransport(api_key="test-key")
97+
try:
98+
response = _build_response(500, '{"message":{"detail":"failed"}}')
99+
100+
with pytest.raises(HyperbrowserError, match="detail"):
101+
transport._handle_response(response)
102+
finally:
103+
transport.close()
104+
105+
106+
def test_async_handle_response_with_non_string_message_field_coerces_to_string():
107+
async def run() -> None:
108+
transport = AsyncTransport(api_key="test-key")
109+
try:
110+
response = _build_response(500, '{"message":{"detail":"failed"}}')
111+
112+
with pytest.raises(HyperbrowserError, match="detail"):
113+
await transport._handle_response(response)
114+
finally:
115+
await transport.close()
116+
117+
asyncio.run(run())

0 commit comments

Comments
 (0)