From c41bcc33041adc488fc020cf48e54f59adad85dd Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Mon, 1 Jul 2024 22:22:23 +0300 Subject: [PATCH] Catch errors retrieving FX rates and return cached version --- agenda/fx.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/agenda/fx.py b/agenda/fx.py index 90cbc5f..efd4d7d 100644 --- a/agenda/fx.py +++ b/agenda/fx.py @@ -90,10 +90,14 @@ def get_rates(config: flask.config.Config) -> dict[str, Decimal]: except httpx.ConnectError: 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: file.write(response.text) - data = json.loads(response.text, parse_float=Decimal) return { cur: Decimal(data["quotes"][f"GBP{cur}"]) for cur in currencies