Repository navigation
fix: raise ClientError when a 4xx JSON body lacks code/msg - #344
Open
saravanan619 wants to merge 2 commits into
Open
saravanan619 wants to merge 2 commits into
saravanan619 wants to merge 2 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
On a 4xx response with a JSON body,
API._handle_exception(hyperliquid/api.py) diderr.get("data")and then unconditionally accessederr["code"]/err["msg"]. If the body has neither key (e.g.{"error": "something"}), the caller getsKeyError: 'code'instead of the documentedClientError, so a bot'sexcept ClientErrorhandling never runs and the process can crash on an error path. Non-object JSON bodies (array, string, number) failed the same way withAttributeErrorfromerr.get("data").Fix
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 aClientErrorwhose message is the raw body).nullbody: raiseClientError(status, None, response.text, ...).code/msg/databehave exactly as before.Tests
Added
tests/api_test.py(9 tests, no network, fake responses): JSON withoutcode/msg, onlycode, onlymsg, fullcode/msg/data, non-dict JSON bodies,nullbody, non-JSON body, 2xx no-raise, and 5xxServerError.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 intests/info_test.pyare pre-existing on the clean tree too (stale cassettes / live API drift), unrelated to this change. Black clean.Fixes #342