From 001d0fb730a62300e867b5fd7df17b3cc22df084 Mon Sep 17 00:00:00 2001 From: Saravanan <76141249+saravanan619@users.noreply.github.com> Date: Wed, 18 Nov 2026 09:45:09 +0530 Subject: [PATCH 1/2] fix: raise ClientError when a 4xx JSON body lacks code/msg (#342) --- hyperliquid/api.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/hyperliquid/api.py b/hyperliquid/api.py index d808f2c..d92fffc 100644 --- a/hyperliquid/api.py +++ b/hyperliquid/api.py @@ -36,8 +36,9 @@ def _handle_exception(self, response): err = json.loads(response.text) except JSONDecodeError: raise ClientError(status_code, None, response.text, None, response.headers) - if err is None: + if err is None or not isinstance(err, dict): raise ClientError(status_code, None, response.text, None, response.headers) error_data = err.get("data") - raise ClientError(status_code, err["code"], err["msg"], response.headers, error_data) + error_message = err.get("msg") or response.text + raise ClientError(status_code, err.get("code"), error_message, response.headers, error_data) raise ServerError(status_code, response.text) From e48d0562589550f6a73c93558901aa483ff1c694 Mon Sep 17 00:00:00 2001 From: Saravanan <76141249+saravanan619@users.noreply.github.com> Date: Wed, 18 Nov 2026 09:45:12 +0530 Subject: [PATCH 2/2] fix: raise ClientError when a 4xx JSON body lacks code/msg (#342) --- tests/api_test.py | 82 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 tests/api_test.py diff --git a/tests/api_test.py b/tests/api_test.py new file mode 100644 index 0000000..b7fdd8b --- /dev/null +++ b/tests/api_test.py @@ -0,0 +1,82 @@ +import pytest + +from hyperliquid.api import API +from hyperliquid.utils.error import ClientError, ServerError + + +class FakeResponse: + def __init__(self, status_code, text): + self.status_code = status_code + self.text = text + self.headers = {} + + +def test_handle_exception_json_without_code_or_msg_raises_client_error(): + # https://github.com/hyperliquid-dex/hyperliquid-python-sdk/issues/342 + # A 4xx JSON body without "code"/"msg" used to raise KeyError("code") + # instead of the documented ClientError. + with pytest.raises(ClientError) as exc_info: + API()._handle_exception(FakeResponse(400, '{"error": "something"}')) + assert exc_info.value.status_code == 400 + assert exc_info.value.error_code is None + assert exc_info.value.error_message == '{"error": "something"}' + + +def test_handle_exception_json_with_only_code_raises_client_error(): + with pytest.raises(ClientError) as exc_info: + API()._handle_exception(FakeResponse(400, '{"code": 123}')) + assert exc_info.value.status_code == 400 + assert exc_info.value.error_code == 123 + assert exc_info.value.error_message == '{"code": 123}' + + +def test_handle_exception_json_with_only_msg_raises_client_error(): + with pytest.raises(ClientError) as exc_info: + API()._handle_exception(FakeResponse(400, '{"msg": "bad request"}')) + assert exc_info.value.status_code == 400 + assert exc_info.value.error_code is None + assert exc_info.value.error_message == "bad request" + + +def test_handle_exception_json_with_code_msg_and_data(): + with pytest.raises(ClientError) as exc_info: + API()._handle_exception(FakeResponse(400, '{"code": 123, "msg": "bad request", "data": {"x": 1}}')) + assert exc_info.value.status_code == 400 + assert exc_info.value.error_code == 123 + assert exc_info.value.error_message == "bad request" + assert exc_info.value.error_data == {"x": 1} + + +def test_handle_exception_non_dict_json_body_raises_client_error(): + # A JSON body that is not an object (e.g. an array or a string) used to + # raise AttributeError from err.get("data"). + for text in ('["not", "a", "dict"]', '"just a string"', "42"): + with pytest.raises(ClientError) as exc_info: + API()._handle_exception(FakeResponse(400, text)) + assert exc_info.value.status_code == 400 + assert exc_info.value.error_message == text + + +def test_handle_exception_null_json_body_raises_client_error(): + with pytest.raises(ClientError) as exc_info: + API()._handle_exception(FakeResponse(400, "null")) + assert exc_info.value.status_code == 400 + assert exc_info.value.error_message == "null" + + +def test_handle_exception_non_json_body_raises_client_error(): + with pytest.raises(ClientError) as exc_info: + API()._handle_exception(FakeResponse(422, "plain text body")) + assert exc_info.value.status_code == 422 + assert exc_info.value.error_code is None + assert exc_info.value.error_message == "plain text body" + + +def test_handle_exception_success_status_does_not_raise(): + API()._handle_exception(FakeResponse(200, '{"ok": true}')) + + +def test_handle_exception_server_error(): + with pytest.raises(ServerError) as exc_info: + API()._handle_exception(FakeResponse(500, "boom")) + assert exc_info.value.status_code == 500