Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(MO-10789): improve error logging of unexpected HTTP responses #165

Merged
merged 2 commits into from
Dec 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')}")
Loading