Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9cfb65a

Browse files
committed
[Proxies] handle aiohttp_socks errors
1 parent 8f97250 commit 9cfb65a

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

tests/exchanges/test_exchange.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import trading_backend.exchanges as exchanges
2121
import trading_backend.errors
2222
import trading_backend.enums
23+
from trading_backend.exchanges.exchange import ProxyConnectionError
2324
from tests import default_exchange
2425

2526

@@ -49,6 +50,12 @@ async def test_is_valid_account(default_exchange):
4950
with pytest.raises(trading_backend.errors.APIKeyPermissionsError):
5051
assert await exchange.is_valid_account() == (True, None)
5152
fetch_balance_mock.assert_called_once()
53+
with mock.patch.object(exchange._exchange.connector.client, "fetch_balance",
54+
mock.AsyncMock(side_effect=ProxyConnectionError)) as fetch_balance_mock:
55+
# non ccxt error: proceed to right checks and raise
56+
with pytest.raises(trading_backend.errors.UnexpectedError):
57+
assert await exchange.is_valid_account() == (True, None)
58+
fetch_balance_mock.assert_called_once()
5259
with mock.patch.object(exchange._exchange.connector.client, "fetch_balance",
5360
mock.AsyncMock(side_effect=ccxt.InvalidNonce)) as fetch_balance_mock:
5461
with pytest.raises(trading_backend.errors.TimeSyncError):

trading_backend/exchanges/exchange.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
# License along with this library.
1616
import ccxt
1717
import contextlib
18+
try:
19+
from aiohttp_socks import ProxyConnectionError
20+
except ImportError:
21+
# local mock in case aiohttp_socks is not available
22+
class ProxyConnectionError(Exception):
23+
pass
1824

1925
import trading_backend.errors
2026
import trading_backend.enums
@@ -199,7 +205,7 @@ def error_describer(self):
199205
"""
200206
try:
201207
yield
202-
except (ccxt.ExchangeNotAvailable, ccxt.AuthenticationError) as err:
208+
except (ccxt.ExchangeNotAvailable, ccxt.AuthenticationError, ProxyConnectionError) as err:
203209
try:
204210
self._exchange.connector.raise_or_prefix_proxy_error_if_relevant(err, None)
205211
except ccxt.BaseError:

0 commit comments

Comments
 (0)