Skip to content

Commit

Permalink
fix: Improve LagoApiError (#272)
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-pochet authored Sep 9, 2024
1 parent 44f9a0e commit 6c275cd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lago_python_client/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ def __init__(
self.response = response
self.detail = detail
self.headers = headers
super().__init__(self.response)

def __repr__(self) -> str:
class_name = self.__class__.__name__
return f"{class_name}(status_code={self.status_code!r}, detail={self.detail!r})"
return f"{class_name}(response={self.response!r}"
6 changes: 6 additions & 0 deletions tests/fixtures/event_unprocessable_entity.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"status": 422,
"error": "Unprocessable Entity",
"code": "validation_errors",
"error_details": { "transaction_id": ["value_already_exist"] }
}
14 changes: 11 additions & 3 deletions tests/test_event_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ def mock_response():
return event_response.read()


def mock_unprocessable_entity_response():
this_dir = os.path.dirname(os.path.abspath(__file__))
data_path = os.path.join(this_dir, "fixtures/event_unprocessable_entity.json")

with open(data_path, "rb") as unprocessable_entity_response:
return unprocessable_entity_response.read()


def mock_fees_response():
this_dir = os.path.dirname(os.path.abspath(__file__))
data_path = os.path.join(this_dir, "fixtures/fees.json")
Expand All @@ -68,13 +76,13 @@ def test_valid_create_events_request_with_string_timestamp(httpx_mock: HTTPXMock


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

httpx_mock.add_response(
method="POST",
url="https://api.getlago.com/api/v1/events",
status_code=401,
content=b"",
status_code=422,
content=mock_unprocessable_entity_response(),
)

with pytest.raises(LagoApiError):
Expand Down

0 comments on commit 6c275cd

Please sign in to comment.