1
0

exchange_rate: guard against garbage hist data coming from exchange

See discussion at 583089d57b (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.
This commit is contained in:
SomberNight
2023-03-16 16:11:02 +00:00
parent 7207f13e97
commit 0a5d18634c

View File

@@ -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