diff --git a/barte/client.py b/barte/client.py index f6c2037..dbc7700 100644 --- a/barte/client.py +++ b/barte/client.py @@ -110,7 +110,7 @@ def create_order(self, data: Union[Dict[str, Any], OrderPayload]) -> Order: error_response = from_dict( data_class=ErrorResponse, data=json_response, config=DACITE_CONFIG ) - error_response.raise_exception() + error_response.raise_exception(response=json_response) return from_dict(data_class=Order, data=json_response, config=DACITE_CONFIG) diff --git a/barte/exceptions.py b/barte/exceptions.py index d4c0aff..b49dc7b 100644 --- a/barte/exceptions.py +++ b/barte/exceptions.py @@ -1,11 +1,14 @@ class BarteError(Exception): """Exception raised for errors in the Barte API.""" - def __init__(self, message, action=None, code=None, charge_uuid=None): + def __init__( + self, message, action=None, code=None, charge_uuid=None, response=None + ): self.message = message self.action = action self.code = code self.charge_uuid = charge_uuid + self.response = response super().__init__(self.message) def __str__(self): diff --git a/barte/models.py b/barte/models.py index 1852cb1..1790ce9 100644 --- a/barte/models.py +++ b/barte/models.py @@ -335,11 +335,11 @@ class ErrorAdditionalInfo: @dataclass class ErrorItem: - status: str code: str title: str description: str - action: str + action: Optional[str] = "" + status: Optional[str] = "" additionalInfo: Optional[ErrorAdditionalInfo] = None @@ -355,12 +355,14 @@ class ErrorResponse: errors: List[ErrorItem] metadata: ErrorMetadata - def raise_exception(self): + def raise_exception(self, response=None): error = self.errors[0] + raise BarteError( message=error.description, action=error.action, code=error.code, + response=response, charge_uuid=error.additionalInfo.chargeUUID if error.additionalInfo else None, diff --git a/tests/test_client.py b/tests/test_client.py index a478afc..5695a5a 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -85,6 +85,24 @@ def mock_order_error_response(): } +@pytest.fixture +def mock_order_unauthorized_error_response(): + return { + "errors": [ + { + "code": "UNAUTHORIZED", + "title": "UNAUTHORIZED", + "description": "Unauthorized", + } + ], + "metadata": { + "totalRecords": 1, + "totalPages": 1, + "requestDatetime": "2025-04-15T10:34:29.576147084-03:00[America/Sao_Paulo]", + }, + } + + @pytest.fixture def mock_charge_response(): return { @@ -358,6 +376,23 @@ def test_create_order_with_invalid_card( ) assert exc_info.value.message == "Erro no Pagamento" + @patch("barte.client.requests.Session.request") + def test_create_order_with_error_unauthorized( + self, mock_request, barte_client, mock_order_unauthorized_error_response + ): + """Test creating a new order with invalid card""" + mock_request.return_value.json.return_value = ( + mock_order_unauthorized_error_response + ) + mock_request.return_value.raise_for_status = Mock() + + with pytest.raises(BarteError) as exc_info: + barte_client.create_order({}) + + assert exc_info.value.code == "UNAUTHORIZED" + assert exc_info.value.message == "Unauthorized" + assert exc_info.value.charge_uuid is None + @patch("barte.client.requests.Session.request") def test_create_card_token(self, mock_request, barte_client): """Test creating a card token using create_card_token"""