Catch errors from external service and display in alert box

Closes: #129
This commit is contained in:
Edward Betts 2024-02-18 22:36:15 +00:00
parent f19e4e4dd4
commit 7a9fbcec7b
2 changed files with 21 additions and 5 deletions

View file

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

View file

@ -127,6 +127,14 @@
Sunset: {{ sunset.strftime("%H:%M:%S") }}</li>
</ul>
{% if errors %}
{% for error in errors %}
<div class="alert alert-danger" role="alert">
Error: {{ error }}
</div>
{% endfor %}
{% endif %}
<h3>Stock markets</h3>
{% for market in stock_markets %}
<p>{{ market }}</p>