Skip to content

Commit 6c275cd

Browse files
fix: Improve LagoApiError (#272)
1 parent 44f9a0e commit 6c275cd

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

lago_python_client/exceptions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ def __init__(
2424
self.response = response
2525
self.detail = detail
2626
self.headers = headers
27+
super().__init__(self.response)
2728

2829
def __repr__(self) -> str:
2930
class_name = self.__class__.__name__
30-
return f"{class_name}(status_code={self.status_code!r}, detail={self.detail!r})"
31+
return f"{class_name}(response={self.response!r}"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"status": 422,
3+
"error": "Unprocessable Entity",
4+
"code": "validation_errors",
5+
"error_details": { "transaction_id": ["value_already_exist"] }
6+
}

tests/test_event_client.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ def mock_response():
4545
return event_response.read()
4646

4747

48+
def mock_unprocessable_entity_response():
49+
this_dir = os.path.dirname(os.path.abspath(__file__))
50+
data_path = os.path.join(this_dir, "fixtures/event_unprocessable_entity.json")
51+
52+
with open(data_path, "rb") as unprocessable_entity_response:
53+
return unprocessable_entity_response.read()
54+
55+
4856
def mock_fees_response():
4957
this_dir = os.path.dirname(os.path.abspath(__file__))
5058
data_path = os.path.join(this_dir, "fixtures/fees.json")
@@ -68,13 +76,13 @@ def test_valid_create_events_request_with_string_timestamp(httpx_mock: HTTPXMock
6876

6977

7078
def test_invalid_create_events_request(httpx_mock: HTTPXMock):
71-
client = Client(api_key="invalid")
79+
client = Client(api_key="886fe239-927d-4072-ab72-6dd345e8dd0d")
7280

7381
httpx_mock.add_response(
7482
method="POST",
7583
url="https://api.getlago.com/api/v1/events",
76-
status_code=401,
77-
content=b"",
84+
status_code=422,
85+
content=mock_unprocessable_entity_response(),
7886
)
7987

8088
with pytest.raises(LagoApiError):

0 commit comments

Comments
 (0)