Skip to content

Fix ClientError arguments and crashes on unusable 4xx bodies - #318

Open
pucedoteth wants to merge 1 commit into
hyperliquid-dex:masterfrom
pucedoteth:fix/handle-exception-client-error
Open

Fix ClientError arguments and crashes on unusable 4xx bodies#318
pucedoteth wants to merge 1 commit into
hyperliquid-dex:masterfrom
pucedoteth:fix/handle-exception-client-error

Conversation

@pucedoteth

Copy link
Copy Markdown

Two problems in API._handle_exception, both in the 4xx branch.

1. The fallback raises pass arguments in the wrong positions

ClientError.__init__ is (status_code, error_code, error_message, header, error_data), but both fallbacks pass:

raise ClientError(status_code, None, response.text, None, response.headers)

so header is None and the HTTP headers land in error_data. The success path passes them correctly, so the same exception type carries headers in a different attribute depending on what the body looked like.

This is the ordinary path, not an edge case. The API answers a bad request with a plain-text body, which is not valid JSON, so every real 4xx takes the JSONDecodeError fallback:

$ curl -s -X POST -H 'Content-Type: application/json' \
    -d '{"type":"nonexistentType"}' https://api.hyperliquid-testnet.xyz/info
Failed to deserialize the JSON body into the target type      # HTTP 422

Anything reading err.header to back off on a rate limit sees None today.

2. Non-object JSON bodies raise the wrong exception type

The body is only checked for None before being used as a mapping. json.loads returns None only for the literal null — a JSON string, array or number is equally not a dict:

body before
"Unauthorized" AttributeError: 'str' object has no attribute 'get'
["a","b"] AttributeError: 'list' object has no attribute 'get'
429 AttributeError: 'int' object has no attribute 'get'
{"error": "rate limited"} KeyError: 'code'

The last row is what an intermediary with its own error schema returns — a gateway, proxy, or the local node at LOCAL_API_URL. In every case the caller gets an exception the SDK does not define, instead of the ClientError they wrapped the call to catch.

Fix

Accept a body only when it is a dict carrying both code and msg; otherwise raise a ClientError holding the raw text as the message, with the headers in header. A well-formed body is unpacked exactly as before.

Testing

Adds tests/api_test.py — the well-formed body is unpacked as before, 5xx still raises ServerError, and eight unusable bodies (including the plain-text one the live API actually returns) all produce a well-formed ClientError.

Reverting only hyperliquid/api.py and keeping the tests: 8 of 11 fail, showing both bugs —

E   AssertionError: assert None == {'x-ratelimit-remaining': '0'}   # x2, the swapped args
E   AttributeError: 'str' object has no attribute 'get'
E   AttributeError: 'list' object has no attribute 'get'
E   AttributeError: 'int' object has no attribute 'get'
E   KeyError: 'code'                                                # x3

Full suite: 47 passed. black --check, isort --check-only and mypy clean.

Doesn't overlap #300, which adds messages to the exception classes in utils/error.py and leaves this function alone.

`API._handle_exception` has two problems, both in the 4xx branch.

The two fallback `raise`s pass their arguments in the wrong positions.
`ClientError.__init__` is `(status_code, error_code, error_message, header,
error_data)`, but they pass `(status_code, None, response.text, None,
response.headers)`, so `header` ends up `None` and the HTTP headers land in
`error_data`. The success path gets this right, so the same exception carries
headers in different attributes depending on the response body.

This is the ordinary path, not an edge case: the API answers a bad request with
a plain-text body ("Failed to deserialize the JSON body into the target type"),
which is not valid JSON, so every real 4xx takes the `JSONDecodeError` fallback
and loses its headers. Anything reading `err.header` to back off on a rate limit
sees `None`.

Second, the body is only checked for `None` before being used as a mapping.
`json.loads` returns `None` only for the literal `null`; a JSON string, array or
number is equally not a dict, and `err.get("data")` raises `AttributeError` on
all three. An object that lacks `code` or `msg` -- as an intermediary with its
own error schema would return -- raises `KeyError`. Either way the caller gets
an exception the SDK does not define, in place of the `ClientError` it catches.

Accept a body only when it is a dict carrying both keys, and otherwise raise a
`ClientError` holding the raw text as the message, with the headers in `header`.

Adds tests/api_test.py: the well-formed body is unpacked as before, 5xx still
raises `ServerError`, and eight unusable bodies -- including the plain-text one
the live API returns -- all produce a well-formed `ClientError`. Eight of the
eleven fail before this change.
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.

1 participant