Skip to content

Commit

Permalink
Improvement to error handling
Browse files Browse the repository at this point in the history
Previously, if error occured during the creation of inner
type instance from iCalendar string, the error with the
name of the bogus property would be stored in the
appropriate component's errors attribute along with the error
string  but the property's value would be removed from the
parsed representation. This patch keeps the value even with its
parameters at that certain property, the value's type is changed
to vText.

Should allow implementation of
collective#158

Improves
collective#174
  • Loading branch information
stlaz committed Aug 12, 2016
1 parent ef64116 commit e49fc98
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/icalendar/cal.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,17 @@ def from_ical(cls, st, multiple=False):
try:
factory = types_factory.for_property(name,
params.get('VALUE'))
if types_factory.is_list_property(name):
except ValueError as e:
if not component.ignore_exceptions:
raise
else:
# add error message and fall back to vText value type
component.errors.append((uname, str(e)))
factory = types_factory['text']
try:
if (types_factory.is_list_property(name) and
factory != vText):
# TODO: list type currenty supports only datetime types
vals = vDDDLists(
vDDDLists.from_ical(vals, factory,
params.get('TZID')))
Expand All @@ -395,10 +405,11 @@ def from_ical(cls, st, multiple=False):
if not component.ignore_exceptions:
raise
component.errors.append((uname, str(e)))
component.add(name, None, encode=0)
else:
vals.params = params
component.add(name, vals, encode=0)
# fallback to vText and store the original value
vals = types_factory['text'](vals)

vals.params = params
component.add(name, vals, encode=0)

if multiple:
return comps
Expand Down
11 changes: 11 additions & 0 deletions src/icalendar/tests/test_fixed_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,13 +466,17 @@ def test_issue_187(self):
event = icalendar.Event.from_ical('\r\n'.join(ical_str))
self.assertEqual(event.errors,
[('MYPROP', "Expected time, got: '20050520T200505'")])
self.assertEqual(event['MYPROP'],
icalendar.prop.vText('20050520T200505'))

# Wrong default property value (DATE instead of DATE-TIME)
ical_str = orig_str[:]
ical_str[2] = 'DTSTART:20150408'
event = icalendar.Event.from_ical('\r\n'.join(ical_str))
self.assertEqual(event.errors,
[('DTSTART', "Wrong datetime format '20150408'")])
self.assertEqual(event['DTSTART'],
icalendar.prop.vText('20150408'))

# -------- Wrong vDDDLists setups follow --------
ical_str = orig_str[:]
Expand All @@ -481,10 +485,17 @@ def test_issue_187(self):
event = icalendar.Event.from_ical('\r\n'.join(ical_str))
self.assertEqual(event.errors,
[('RDATE', "Wrong date format '20150217T095800'")])
self.assertEqual(event['RDATE'],
icalendar.prop.vText('20150217T095800'))

ical_str[3] = ('RDATE;FMTTYPE=text/plain;ENCODING=BASE64;VALUE=BINARY:'
'c3RsYXo=')
event = icalendar.Event.from_ical('\r\n'.join(ical_str))
self.assertEqual(event.errors,
[('RDATE', "The VALUE parameter of RDATE property "
"is not supported: 'BINARY'")])
self.assertEqual(event['RDATE'],
icalendar.prop.vText('c3RsYXo='))
self.assertEqual(event['RDATE'].params,
{'VALUE': 'BINARY', 'ENCODING': 'BASE64',
'FMTTYPE': 'text/plain'})

0 comments on commit e49fc98

Please sign in to comment.