calendar.ical.get_upcoming_events should fail hard if there was any exception/unsuccessful response

The error should not be swallowed and it should be instead propagated up
to calendar.get_upcoming_events, if it's called from that context. And
calendar.get_upcoming_events should be in charge of handling the
exceptions and make sure that failing to retrieve the events for one
calendar doesn't make the whole method fail.
This commit is contained in:
Fabio Manganiello 2021-03-05 11:16:14 +01:00
parent 4ada1c663d
commit 47ba13d985
2 changed files with 22 additions and 31 deletions

View File

@ -110,8 +110,11 @@ class CalendarPlugin(Plugin, CalendarInterface):
events = [] events = []
for calendar in self.calendars: for calendar in self.calendars:
cal_events = calendar.get_upcoming_events().output or [] try:
events.extend(cal_events) cal_events = calendar.get_upcoming_events().output or []
events.extend(cal_events)
except Exception as e:
self.logger.warning('Could not retrieve events: {}'.format(str(e)))
events = sorted(events, key=lambda event: events = sorted(events, key=lambda event:
dateutil.parser.parse( dateutil.parser.parse(

View File

@ -69,38 +69,26 @@ class CalendarIcalPlugin(Plugin, CalendarInterface):
from icalendar import Calendar from icalendar import Calendar
events = [] events = []
try: response = requests.get(self.url)
response = requests.get(self.url) assert response.ok, \
except Exception as e: "HTTP error while getting events from {}: {}".format(self.url, response.text)
self.logger.exception(e)
return events
if response.ok: calendar = Calendar.from_ical(response.text)
calendar = None for event in calendar.walk():
if event.name != 'VEVENT':
continue # Not an event
try: event = self._translate_event(event)
calendar = Calendar.from_ical(response.text)
except Exception as e:
self.logger.exception(e)
return events
for event in calendar.walk(): if event['status'] and event['responseStatus'] \
if event.name != 'VEVENT': and dateutil.parser.parse(event['end']['dateTime']) >= \
continue # Not an event datetime.datetime.now(pytz.timezone('UTC')) \
and (
event = self._translate_event(event) (only_participating
and event['status'] == 'confirmed'
if event['status'] and event['responseStatus'] \ and event['responseStatus'] in ['accepted', 'tentative'])
and dateutil.parser.parse(event['end']['dateTime']) >= \ or not only_participating):
datetime.datetime.now(pytz.timezone('UTC')) \ events.append(event)
and (
(only_participating
and event['status'] == 'confirmed'
and event['responseStatus'] in ['accepted', 'tentative'])
or not only_participating):
events.append(event)
else:
self.logger.error("HTTP error while getting {}: {}".format(self.url, response))
return events return events