Skip to content

fix: raise ClientError when a 4xx JSON body lacks code/msg - #344

Open
saravanan619 wants to merge 2 commits into
hyperliquid-dex:masterfrom
saravanan619:fix/handle-exception-missing-code-msg-342
Open

saravanan619 wants to merge 2 commits into
hyperliquid-dex:masterfrom
saravanan619:fix/handle-exception-missing-code-msg-342

Conversation

@saravanan619

Copy link
Copy Markdown

Bug

On a 4xx response with a JSON body, API._handle_exception (hyperliquid/api.py) did err.get("data") and then unconditionally accessed err["code"] / err["msg"]. If the body has neither key (e.g. {"error": "something"}), the caller gets KeyError: 'code' instead of the documented ClientError, so a bot's except ClientError handling never runs and the process can crash on an error path. Non-object JSON bodies (array, string, number) failed the same way with AttributeError from err.get("data").

Fix

  • Use err.get("code") / err.get("msg") with a fallback to the raw response body as the message, so no information is lost (e.g. {"error": "something"} surfaces as a ClientError whose message is the raw body).
  • Treat a non-dict JSON body the same as a null body: raise ClientError(status, None, response.text, ...).
  • Bodies that do carry code / msg / data behave exactly as before.

Tests

Added tests/api_test.py (9 tests, no network, fake responses): JSON without code/msg, only code, only msg, full code/msg/data, non-dict JSON bodies, null body, non-JSON body, 2xx no-raise, and 5xx ServerError.

Verified red-then-green: 4 of the new tests fail on unpatched code (KeyError/AttributeError) and all 9 pass after the fix. Full suite: 38 passed; the 7 failures in tests/info_test.py are pre-existing on the clean tree too (stale cassettes / live API drift), unrelated to this change. Black clean.

Fixes #342

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

API._handle_exception raises KeyError instead of ClientError on some 4xx responses

1 participant