Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion checkout_sdk/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def invoke(self,
self._logger.error(err)
raise CheckoutApiException(err.response) from err
except OSError as err:
error = err.strerror
error = err.strerror or str(err)
raise CheckoutException(error) from err
finally:
# get_file_request opens the upload; requests reads it but never closes it.
Expand Down
43 changes: 42 additions & 1 deletion tests/exception_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import pytest
from unittest.mock import Mock
import requests
from unittest.mock import MagicMock, Mock
from checkout_sdk.api_client import ApiClient
from checkout_sdk.exception import (
CheckoutException,
CheckoutArgumentException,
Expand Down Expand Up @@ -235,3 +237,42 @@ def test_checkout_api_exception_logs_on_json_parse_error(caplog):
raise CheckoutApiException(response)

assert any("Failed to parse response JSON payload" in m for m in caplog.messages)


def _api_client_failing_with(error):
configuration = MagicMock()
configuration.http_client.request.side_effect = error
api_client = ApiClient(configuration=configuration, base_uri='https://api.sandbox.checkout.com/')
authorization = MagicMock()
authorization.get_authorization_header.return_value = 'Bearer test'
return api_client, authorization


@pytest.mark.parametrize('error, expected', [
(requests.exceptions.ConnectTimeout('Connection to api.checkout.com timed out'),
'Connection to api.checkout.com timed out'),
(requests.exceptions.ConnectionError('Failed to establish a new connection'),
'Failed to establish a new connection'),
(requests.exceptions.ReadTimeout('Read timed out. (read timeout=30)'),
'Read timed out. (read timeout=30)'),
])
def test_checkout_exception_keeps_the_message_of_a_transport_error(error, expected):
# requests' transport errors are OSError subclasses built without errno/strerror,
# so strerror is None and the message has to come from the exception itself.
api_client, authorization = _api_client_failing_with(error)

with pytest.raises(CheckoutException) as exc_info:
api_client.get('/payments/pay_123', authorization)

assert str(exc_info.value) == expected
assert exc_info.value.__cause__ is error


def test_checkout_exception_keeps_strerror_of_an_os_error():
error = FileNotFoundError(2, 'No such file or directory')
api_client, authorization = _api_client_failing_with(error)

with pytest.raises(CheckoutException) as exc_info:
api_client.get('/payments/pay_123', authorization)

assert str(exc_info.value) == 'No such file or directory'