From 0a9a6dfd6c25b1c159d5e08a335562d5a6e81a69 Mon Sep 17 00:00:00 2001 From: Egor fureunoir Gorbunov Date: Fri, 9 May 2025 04:17:47 +0300 Subject: [PATCH] Features: 1) Add error handling with logger and custom error message for holiday API requests; Fixes: (none); Extra: 1) Replace direct API response raising with try-except block for better robustness and logging; --- core/tasks.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/core/tasks.py b/core/tasks.py index f8d9f286..03691f3d 100644 --- a/core/tasks.py +++ b/core/tasks.py @@ -168,11 +168,15 @@ def process_promotions() -> tuple[bool, str]: for day_offset in range(4): checked_date = date.today() + timedelta(days=day_offset) - response = requests.get( - f"https://holidays.abstractapi.com/v1/?api_key={config.ABSTRACT_API_KEY}&country=GB&" - f"month={checked_date.month}&day={checked_date.day}" - ) - response.raise_for_status() + try: + response = requests.get( + f"https://holidays.abstractapi.com/v1/?api_key={config.ABSTRACT_API_KEY}&country=GB&" + f"month={checked_date.month}&day={checked_date.day}" + ) + response.raise_for_status() + except Exception as e: + logger.warning(f"Couldn't fetch holiday data for {checked_date}: {e!s}") + return False, f"Couldn't fetch holiday data for {checked_date}: {e!s}" holidays = response.json() if holidays: holiday_data = holidays[0]