-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathtest_exception_repr.py
More file actions
109 lines (90 loc) · 3.39 KB
/
test_exception_repr.py
File metadata and controls
109 lines (90 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"""Tests for TogetherException.__repr__ with non-JSON-serializable objects.
Regression tests for https://github.com/togethercomputer/together-python/issues/108
where repr() on a TogetherException crashed with TypeError when headers
contained non-serializable objects like aiohttp's CIMultiDictProxy.
"""
from __future__ import annotations
import json
from typing import Any, Iterator
import pytest
from together.error import (
APIConnectionError,
APIError,
AuthenticationError,
JSONError,
RateLimitError,
ResponseError,
Timeout,
TogetherException,
)
class FakeCIMultiDictProxy:
"""Mimics aiohttp's CIMultiDictProxy, which is not JSON-serializable."""
def __init__(self, data: dict[str, str]) -> None:
self._data = data
def __iter__(self) -> Iterator[str]:
return iter(self._data)
def __len__(self) -> int:
return len(self._data)
def __getitem__(self, key: str) -> str:
return self._data[key]
def __repr__(self) -> str:
return f"<CIMultiDictProxy({self._data!r})>"
class TestExceptionReprNonSerializable:
"""repr() must never crash, even with non-JSON-serializable attributes."""
def test_repr_with_non_serializable_headers(self) -> None:
"""Core bug from issue #108: CIMultiDictProxy headers crash repr()."""
headers = FakeCIMultiDictProxy({"Content-Type": "application/json"})
exc = TogetherException(
message="server error",
headers=headers, # type: ignore[arg-type]
http_status=500,
)
# Before fix: TypeError: Object of type FakeCIMultiDictProxy is not
# JSON serializable
result = repr(exc)
assert "TogetherException" in result
assert "server error" in result
def test_repr_with_dict_headers(self) -> None:
"""Normal dict headers must still work (regression check)."""
exc = TogetherException(
message="bad request",
headers={"X-Request-Id": "abc-123"},
http_status=400,
request_id="req-1",
)
result = repr(exc)
parsed = json.loads(result.split("(", 1)[1].rsplit(")", 1)[0].strip("'\""))
assert parsed["status"] == 400
assert parsed["request_id"] == "req-1"
def test_repr_with_none_headers(self) -> None:
"""Default None headers (stored as {}) must work."""
exc = TogetherException(message="oops")
result = repr(exc)
assert "TogetherException" in result
def test_repr_with_string_headers(self) -> None:
"""String headers must work."""
exc = TogetherException(message="err", headers="raw-header")
result = repr(exc)
assert "raw-header" in result
@pytest.mark.parametrize(
"exc_class",
[
AuthenticationError,
ResponseError,
JSONError,
RateLimitError,
Timeout,
APIConnectionError,
APIError,
],
)
def test_subclasses_inherit_fix(self, exc_class: type) -> None:
"""All subclasses inherit the safe repr via TogetherException."""
headers = FakeCIMultiDictProxy({"X-Rate-Limit": "100"})
exc = exc_class(
message="subclass test",
headers=headers, # type: ignore[arg-type]
http_status=429,
)
result = repr(exc)
assert exc_class.__name__ in result