From 0a5d18634c8a2c2ba42c083d7e8d566d9353978d Mon Sep 17 00:00:00 2001 From: SomberNight Date: Thu, 16 Mar 2023 16:11:02 +0000 Subject: [PATCH] exchange_rate: guard against garbage hist data coming from exchange See discussion at https://github.com/spesmilo/electrum/commit/583089d57b517ab001721cfc9e4d883f3d4c4f7c#r104678577 CoinGecko for PLN gives "None" str as rate (instead of null) for two months mid-2014: ``` 2.29 | D | exchange_rate.CoinGecko | found corrupted historical_rate: rate='None'. for ccy='PLN' at 2014-05-10 ``` Thanks to @lukasz1992 for reporting. --- electrum/exchange_rate.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/electrum/exchange_rate.py b/electrum/exchange_rate.py index 87b3996ba..55c9edee0 100644 --- a/electrum/exchange_rate.py +++ b/electrum/exchange_rate.py @@ -161,8 +161,13 @@ class ExchangeBase(Logger): return [] def historical_rate(self, ccy: str, d_t: datetime) -> Decimal: - rate = self._history.get(ccy, {}).get(d_t.strftime('%Y-%m-%d')) or 'NaN' - return Decimal(rate) + date_str = d_t.strftime('%Y-%m-%d') + rate = self._history.get(ccy, {}).get(date_str) or 'NaN' + try: + return Decimal(rate) + except Exception: # guard against garbage coming from exchange + #self.logger.debug(f"found corrupted historical_rate: {rate=!r}. for {ccy=} at {date_str}") + return Decimal('NaN') async def request_history(self, ccy: str) -> Dict[str, Union[str, float]]: raise NotImplementedError() # implemented by subclasses