From 7a9fbcec7bda3448dc9450930b48268f2f693713 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Sun, 18 Feb 2024 22:36:15 +0000 Subject: [PATCH] Catch errors from external service and display in alert box Closes: #129 --- agenda/data.py | 18 +++++++++++++----- templates/index.html | 8 ++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/agenda/data.py b/agenda/data.py index 75043bf..27e2cf1 100644 --- a/agenda/data.py +++ b/agenda/data.py @@ -276,12 +276,15 @@ async def time_function( func: typing.Callable[..., typing.Coroutine[typing.Any, typing.Any, typing.Any]], *args, **kwargs, -) -> tuple[str, typing.Any, float]: +) -> tuple[str, typing.Any, float, Exception | None]: """Time the execution of an asynchronous function.""" - start_time = time() - result = await func(*args, **kwargs) + start_time, result, exception = time(), None, None + try: + result = await func(*args, **kwargs) + except Exception as e: + exception = e end_time = time() - return name, result, end_time - start_time + return name, result, end_time - start_time, exception def gap_list( @@ -344,6 +347,8 @@ async def get_data( results = {call[0]: call[1] for call in result_list} + errors = [(call[0], call[3]) for call in result_list if call[3]] + gwr_advance_tickets = results["gwr_advance_tickets"] data_gather_seconds = time() - t0 @@ -389,7 +394,9 @@ async def get_data( events += accommodation_events events += travel.all_events(my_data) events += conference.get_list(os.path.join(my_data, "conferences.yaml")) - events += results["backwell_bins"] + results["bristol_bins"] + for key in "backwell_bins", "bristol_bins": + if results[key]: + events += results[key] events += read_events_yaml(my_data, last_year, next_year) events += subscription.get_events(os.path.join(my_data, "subscriptions.yaml")) events += economist.publication_dates(last_week, next_year) @@ -467,5 +474,6 @@ async def get_data( reply["two_weeks_ago"] = two_weeks_ago reply["fullcalendar_events"] = calendar.build_events(events) + reply["errors"] = errors return reply diff --git a/templates/index.html b/templates/index.html index 6c452ec..4034a2f 100644 --- a/templates/index.html +++ b/templates/index.html @@ -127,6 +127,14 @@ Sunset: {{ sunset.strftime("%H:%M:%S") }} + {% if errors %} + {% for error in errors %} + + {% endfor %} + {% endif %} +

Stock markets

{% for market in stock_markets %}

{{ market }}