Skip to content

Commit 059b864

Browse files
authored
Merge pull request #33 from buserbrasil/change-error-response-data-class-to-allow-optional-fields
Changes ErrorResponse to allow optional fields to handle Unauthorized error response
2 parents 52266b7 + a783430 commit 059b864

4 files changed

Lines changed: 45 additions & 5 deletions

File tree

barte/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def create_order(self, data: Union[Dict[str, Any], OrderPayload]) -> Order:
110110
error_response = from_dict(
111111
data_class=ErrorResponse, data=json_response, config=DACITE_CONFIG
112112
)
113-
error_response.raise_exception()
113+
error_response.raise_exception(response=json_response)
114114

115115
return from_dict(data_class=Order, data=json_response, config=DACITE_CONFIG)
116116

barte/exceptions.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
class BarteError(Exception):
22
"""Exception raised for errors in the Barte API."""
33

4-
def __init__(self, message, action=None, code=None, charge_uuid=None):
4+
def __init__(
5+
self, message, action=None, code=None, charge_uuid=None, response=None
6+
):
57
self.message = message
68
self.action = action
79
self.code = code
810
self.charge_uuid = charge_uuid
11+
self.response = response
912
super().__init__(self.message)
1013

1114
def __str__(self):

barte/models.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,11 +335,11 @@ class ErrorAdditionalInfo:
335335

336336
@dataclass
337337
class ErrorItem:
338-
status: str
339338
code: str
340339
title: str
341340
description: str
342-
action: str
341+
action: Optional[str] = ""
342+
status: Optional[str] = ""
343343
additionalInfo: Optional[ErrorAdditionalInfo] = None
344344

345345

@@ -355,12 +355,14 @@ class ErrorResponse:
355355
errors: List[ErrorItem]
356356
metadata: ErrorMetadata
357357

358-
def raise_exception(self):
358+
def raise_exception(self, response=None):
359359
error = self.errors[0]
360+
360361
raise BarteError(
361362
message=error.description,
362363
action=error.action,
363364
code=error.code,
365+
response=response,
364366
charge_uuid=error.additionalInfo.chargeUUID
365367
if error.additionalInfo
366368
else None,

tests/test_client.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,24 @@ def mock_order_error_response():
8585
}
8686

8787

88+
@pytest.fixture
89+
def mock_order_unauthorized_error_response():
90+
return {
91+
"errors": [
92+
{
93+
"code": "UNAUTHORIZED",
94+
"title": "UNAUTHORIZED",
95+
"description": "Unauthorized",
96+
}
97+
],
98+
"metadata": {
99+
"totalRecords": 1,
100+
"totalPages": 1,
101+
"requestDatetime": "2025-04-15T10:34:29.576147084-03:00[America/Sao_Paulo]",
102+
},
103+
}
104+
105+
88106
@pytest.fixture
89107
def mock_charge_response():
90108
return {
@@ -358,6 +376,23 @@ def test_create_order_with_invalid_card(
358376
)
359377
assert exc_info.value.message == "Erro no Pagamento"
360378

379+
@patch("barte.client.requests.Session.request")
380+
def test_create_order_with_error_unauthorized(
381+
self, mock_request, barte_client, mock_order_unauthorized_error_response
382+
):
383+
"""Test creating a new order with invalid card"""
384+
mock_request.return_value.json.return_value = (
385+
mock_order_unauthorized_error_response
386+
)
387+
mock_request.return_value.raise_for_status = Mock()
388+
389+
with pytest.raises(BarteError) as exc_info:
390+
barte_client.create_order({})
391+
392+
assert exc_info.value.code == "UNAUTHORIZED"
393+
assert exc_info.value.message == "Unauthorized"
394+
assert exc_info.value.charge_uuid is None
395+
361396
@patch("barte.client.requests.Session.request")
362397
def test_create_card_token(self, mock_request, barte_client):
363398
"""Test creating a card token using create_card_token"""

0 commit comments

Comments
 (0)