Skip to content

Commit b9dbe82

Browse files
Improve JSON error payload fallback message extraction
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 6693f71 commit b9dbe82

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

hyperbrowser/transport/error_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ def extract_error_message(response: httpx.Response, fallback_error: Exception) -
2929
message = error_data.get(key)
3030
if message is not None:
3131
return _stringify_error_value(message)
32-
return response.text or str(fallback_error)
32+
return _stringify_error_value(error_data)
3333
if isinstance(error_data, str):
3434
return error_data
35-
return _stringify_error_value(response.text or str(fallback_error))
35+
return _stringify_error_value(error_data)
3636

3737

3838
def extract_request_error_context(error: httpx.RequestError) -> tuple[str, str]:

tests/test_transport_response_handling.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,33 @@ async def run() -> None:
211211
asyncio.run(run())
212212

213213

214+
def test_sync_handle_response_with_dict_without_message_keys_stringifies_payload():
215+
transport = SyncTransport(api_key="test-key")
216+
try:
217+
response = _build_response(500, '{"code":"UPSTREAM_FAILURE","retryable":false}')
218+
219+
with pytest.raises(HyperbrowserError, match='"code": "UPSTREAM_FAILURE"'):
220+
transport._handle_response(response)
221+
finally:
222+
transport.close()
223+
224+
225+
def test_async_handle_response_with_non_dict_json_stringifies_payload():
226+
async def run() -> None:
227+
transport = AsyncTransport(api_key="test-key")
228+
try:
229+
response = _build_response(500, '[{"code":"UPSTREAM_FAILURE"}]')
230+
231+
with pytest.raises(
232+
HyperbrowserError, match='\\[\\{"code": "UPSTREAM_FAILURE"\\}\\]'
233+
):
234+
await transport._handle_response(response)
235+
finally:
236+
await transport.close()
237+
238+
asyncio.run(run())
239+
240+
214241
def test_sync_transport_post_wraps_request_errors_with_url_context():
215242
transport = SyncTransport(api_key="test-key")
216243
original_post = transport.client.post

0 commit comments

Comments
 (0)