From 47ba13d9855a75b705f88dba702390a5412d4ccf Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Fri, 5 Mar 2021 11:16:14 +0100 Subject: [PATCH] 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. --- platypush/plugins/calendar/__init__.py | 7 ++-- platypush/plugins/calendar/ical.py | 46 ++++++++++---------------- 2 files changed, 22 insertions(+), 31 deletions(-) diff --git a/platypush/plugins/calendar/__init__.py b/platypush/plugins/calendar/__init__.py index d47e1dac97..377848b254 100644 --- a/platypush/plugins/calendar/__init__.py +++ b/platypush/plugins/calendar/__init__.py @@ -110,8 +110,11 @@ class CalendarPlugin(Plugin, CalendarInterface): events = [] for calendar in self.calendars: - cal_events = calendar.get_upcoming_events().output or [] - events.extend(cal_events) + try: + 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: dateutil.parser.parse( diff --git a/platypush/plugins/calendar/ical.py b/platypush/plugins/calendar/ical.py index a7222508cb..792d1cc354 100644 --- a/platypush/plugins/calendar/ical.py +++ b/platypush/plugins/calendar/ical.py @@ -69,38 +69,26 @@ class CalendarIcalPlugin(Plugin, CalendarInterface): from icalendar import Calendar events = [] - try: - response = requests.get(self.url) - except Exception as e: - self.logger.exception(e) - return events + response = requests.get(self.url) + assert response.ok, \ + "HTTP error while getting events from {}: {}".format(self.url, response.text) - if response.ok: - calendar = None + calendar = Calendar.from_ical(response.text) + for event in calendar.walk(): + if event.name != 'VEVENT': + continue # Not an event - try: - calendar = Calendar.from_ical(response.text) - except Exception as e: - self.logger.exception(e) - return events + event = self._translate_event(event) - for event in calendar.walk(): - if event.name != 'VEVENT': - continue # Not an event - - event = self._translate_event(event) - - if event['status'] and event['responseStatus'] \ - and dateutil.parser.parse(event['end']['dateTime']) >= \ - datetime.datetime.now(pytz.timezone('UTC')) \ - 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)) + if event['status'] and event['responseStatus'] \ + and dateutil.parser.parse(event['end']['dateTime']) >= \ + datetime.datetime.now(pytz.timezone('UTC')) \ + and ( + (only_participating + and event['status'] == 'confirmed' + and event['responseStatus'] in ['accepted', 'tentative']) + or not only_participating): + events.append(event) return events