Catch errors retrieving FX rates and return cached version

This commit is contained in:
Edward Betts 2024-07-01 22:22:23 +03:00
parent 01b42845c3
commit c41bcc3304

View file

@ -90,10 +90,14 @@ def get_rates(config: flask.config.Config) -> dict[str, Decimal]:
except httpx.ConnectError: except httpx.ConnectError:
return read_cached_rates(full_path, currencies) return read_cached_rates(full_path, currencies)
try:
data = json.loads(response.text, parse_float=Decimal)
except json.decoder.JSONDecodeError:
return read_cached_rates(full_path, currencies)
with open(os.path.join(fx_dir, filename), "w") as file: with open(os.path.join(fx_dir, filename), "w") as file:
file.write(response.text) file.write(response.text)
data = json.loads(response.text, parse_float=Decimal)
return { return {
cur: Decimal(data["quotes"][f"GBP{cur}"]) cur: Decimal(data["quotes"][f"GBP{cur}"])
for cur in currencies for cur in currencies