Merge pull request #1057 from freqtrade/fix/fiatconvert_error

Catch all exceptions from fiat-convert api calls
This commit is contained in:
Samuel Husso 2018-07-21 23:12:56 -05:00 committed by GitHub
commit 849ded7772
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 3 deletions

View File

@ -7,7 +7,6 @@ import logging
import time import time
from typing import Dict, List from typing import Dict, List
from requests.exceptions import RequestException
from coinmarketcap import Market from coinmarketcap import Market
from freqtrade.constants import SUPPORTED_FIAT from freqtrade.constants import SUPPORTED_FIAT
@ -90,10 +89,10 @@ class CryptoToFiatConverter(object):
coinlistings = self._coinmarketcap.listings() coinlistings = self._coinmarketcap.listings()
self._cryptomap = dict(map(lambda coin: (coin["symbol"], str(coin["id"])), self._cryptomap = dict(map(lambda coin: (coin["symbol"], str(coin["id"])),
coinlistings["data"])) coinlistings["data"]))
except (ValueError, RequestException) as exception: except (BaseException) as exception:
logger.error( logger.error(
"Could not load FIAT Cryptocurrency map for the following problem: %s", "Could not load FIAT Cryptocurrency map for the following problem: %s",
exception type(exception).__name__
) )
def convert_amount(self, crypto_amount: float, crypto_symbol: str, fiat_symbol: str) -> float: def convert_amount(self, crypto_amount: float, crypto_symbol: str, fiat_symbol: str) -> float:

View File

@ -183,6 +183,24 @@ def test_fiat_convert_without_network(mocker):
CryptoToFiatConverter._coinmarketcap = cmc_temp CryptoToFiatConverter._coinmarketcap = cmc_temp
def test_fiat_invalid_response(mocker, caplog):
# Because CryptoToFiatConverter is a Singleton we reset the listings
listmock = MagicMock(return_value="{'novalidjson':DEADBEEFf}")
mocker.patch.multiple(
'freqtrade.fiat_convert.Market',
listings=listmock,
)
# with pytest.raises(RequestEsxception):
fiat_convert = CryptoToFiatConverter()
fiat_convert._cryptomap = {}
fiat_convert._load_cryptomap()
length_cryptomap = len(fiat_convert._cryptomap)
assert length_cryptomap == 0
assert log_has('Could not load FIAT Cryptocurrency map for the following problem: TypeError',
caplog.record_tuples)
def test_convert_amount(mocker): def test_convert_amount(mocker):
patch_coinmarketcap(mocker) patch_coinmarketcap(mocker)
mocker.patch('freqtrade.fiat_convert.CryptoToFiatConverter.get_price', return_value=12345.0) mocker.patch('freqtrade.fiat_convert.CryptoToFiatConverter.get_price', return_value=12345.0)