Skip to content
Open
Show file tree
Hide file tree
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
7 changes: 6 additions & 1 deletion sdks/python/pmxt/_hosted_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import json
import logging
from collections.abc import Callable, Mapping, Sequence
from typing import ClassVar, TYPE_CHECKING

Expand All @@ -21,6 +22,9 @@
import httpx


logger = logging.getLogger(__name__)


class HostedTradingError(PmxtError):
"""Root error for hosted trading failures returned by trade.pmxt.dev."""

Expand Down Expand Up @@ -178,7 +182,8 @@ def _matches_status(expected_status: int | None, status: int) -> bool:
def _json_payload(response: httpx.Response) -> object | None:
try:
return response.json()
except ValueError:
except ValueError as exc:
logger.debug("Could not parse hosted response body as JSON: %s", exc)
return None


Expand Down
15 changes: 15 additions & 0 deletions sdks/python/tests/test_hosted_errors.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logging

import httpx
import pytest

Expand Down Expand Up @@ -256,3 +258,16 @@ def test_hosted_exceptions_also_catch_as_legacy_base(error_class, legacy_base):
assert issubclass(error_class, legacy_base)
assert issubclass(error_class, HostedTradingError)
assert issubclass(error_class, PmxtError)


def test_malformed_response_body_is_logged_at_debug(caplog):
response = _response(500, text_body="<html>502 Bad Gateway</html>")

with caplog.at_level(logging.DEBUG, logger="pmxt._hosted_errors"):
with pytest.raises(HostedTradingError):
raise_from_response(response)

assert any(
"Could not parse hosted response body as JSON" in record.message
for record in caplog.records
)