Skip to content

Commit

Permalink
chore(MO-10789): improve error logging of unexpected HTTP responses (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jnicoulaud-ledger authored Dec 6, 2024
1 parent b68ceac commit 4c86f68
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/erc7730/common/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def get(model: type[_T], url: HttpUrl | FileUrl, **params: Any) -> _T:
try:
return TypeAdapter(model).validate_json(response)
except ValidationError as e:
raise Exception(f"Received unexpected response from {url}: {response.decode()}") from e
raise Exception(f"Received unexpected response from {url}: {response.decode(errors='replace')}") from e


def _client() -> Client:
Expand Down Expand Up @@ -184,8 +184,11 @@ def handle_request(self, request: Request) -> Response:
response.close()

# unwrap result, sometimes containing JSON directly, sometimes JSON in a string
if (result := response.json().get("result")) is not None:
data = result if isinstance(result, str) else json.dumps(result)
return Response(status_code=response.status_code, stream=IteratorByteStream([data.encode()]))

raise Exception(f"Unexpected response from Etherscan: {response.content}")
try:
if (result := response.json().get("result")) is not None:
data = result if isinstance(result, str) else json.dumps(result)
return Response(status_code=response.status_code, stream=IteratorByteStream([data.encode()]))
except Exception:
pass # nosec B110 - intentional try/except/pass

raise Exception(f"Unexpected response from Etherscan: {response.content.decode(errors='replace')}")

0 comments on commit 4c86f68

Please sign in to comment.