From 1577fd8d4ad369ac8f8c1b2cb4055d96e7416be0 Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 28 Nov 2026 08:35:52 -0400 Subject: [PATCH] fix(client): keep the message of timeouts and connection errors requests' transport errors are OSError subclasses built without errno/strerror, so err.strerror is None and CheckoutException ended up with an empty message. Fall back to str(err) when strerror is missing. Co-Authored-By: Claude Fable 5.2 --- checkout_sdk/api_client.py | 2 +- tests/exception_test.py | 43 +++++++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/checkout_sdk/api_client.py b/checkout_sdk/api_client.py index 7b2a7c63..db51ea89 100644 --- a/checkout_sdk/api_client.py +++ b/checkout_sdk/api_client.py @@ -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. diff --git a/tests/exception_test.py b/tests/exception_test.py index 9f3aa2d9..df92cd8b 100644 --- a/tests/exception_test.py +++ b/tests/exception_test.py @@ -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, @@ -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'