From c08530843c7f807d9efe0e4ea87d7e625861a109 Mon Sep 17 00:00:00 2001 From: Tony Breeds Date: Thu, 8 Oct 2015 15:11:04 +1100 Subject: [PATCH] Clarify KeyError() in Schedule.__init__() If a YAML file contains an invalid recurrence the error is: --- Invalid YAML meeting schedule definition - missing attribute 'weekly.' < stack strace > KeyError: 'weekly.' --- This is quite right 'weekly.' isn't a missing attribute it's an invalid recurrence type. Refactor the code slightly to clarify the error message --- Invalid meeting recurrence 'weekly.' - valid types: ['biweekly-odd', 'biweekly-even', 'weekly'] < stack strace > KeyError: 'weekly.' --- Change-Id: I8618bba454037744b82b5b96175d71ef8b2a652f --- yaml2ical/meeting.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/yaml2ical/meeting.py b/yaml2ical/meeting.py index 8d4532e..c7541c6 100644 --- a/yaml2ical/meeting.py +++ b/yaml2ical/meeting.py @@ -61,12 +61,21 @@ class Schedule(object): self.day = sched_yaml['day'].lower().capitalize() self.irc = sched_yaml['irc'] self.freq = sched_yaml['frequency'] - self.recurrence = supported_recurrences[sched_yaml['frequency']] + self._recurrence = sched_yaml['frequency'] except KeyError as e: print("Invalid YAML meeting schedule definition - missing " "attribute '{0}'".format(e.args[0])) raise + # Validate inputs + try: + self.recurrence = supported_recurrences[self._recurrence] + except KeyError as e: + print("Invalid meeting recurrence '{0}' - " + "valid types: {1}".format(e.args[0], + supported_recurrences.keys())) + raise + # optional: start_date defaults to the current date if not present if 'start_date' in sched_yaml: try: