Ignore Exceptions When Loading Invalid Data in Python
Email files created by different applications do not always follow the specifications. For example, an iCalendar file may contain a recurrence rule with an invalid value. By default, Aspose.Email throws an exception for such data. For some of these cases, the library can skip the invalid part and continue loading.
The ExceptionManager class in the aspose.email.exceptions module controls this behavior. It lets you ignore all ignorable exceptions or only selected ones.
Recognize an Ignorable Exception
When an exception can be ignored, its message tells you so and shows the path of this case. For example, loading an ICS file that contains RRULE:FREQ=DAILY;INTERVAL=0;COUNT=3 with Appointment.load raises a RuntimeError with the following message:
Proxy error(AsposeException): Incorrect option value of a recurrent rule: 'INTERVAL'.
If you want to ignore an exception and want to proceed further then you can use:
ExceptionManager.IgnoreList.Add(@"RecurrenceRule\IncorrectIntervalRulePart")
The INTERVAL rule part should be a positive integer.
The first line describes the problem. The ExceptionManager.IgnoreList.Add(...) line gives the path of this case in C# syntax: in Python, pass the same path to ExceptionManager.ignore_list.append. The last line explains what the library does when the exception is ignored.
What Can Be Ignored
- Only exceptions whose message contains the
ExceptionManager.IgnoreList.Add(...)hint can be ignored. All other exceptions are always raised, whatever theExceptionManagersettings are. This includes argument validation, such as an empty file name, a malformed email address, or a missing file. - The library currently supports the following cases:
- An invalid rule part in an iCalendar recurrence rule, for example,
INTERVAL=0(pathRecurrenceRule\IncorrectIntervalRulePart). When ignored, the invalid rule part is dropped:FREQ=DAILY;INTERVAL=0;COUNT=3is loaded asFREQ=DAILY;COUNT=3. - A damaged TNEF attachment (
winmail.dat) when loading a MailMessage (pathMailMessage\Load\DecodeTnefAttachment). - Damaged data headers when reading OLM files.
- An invalid rule part in an iCalendar recurrence rule, for example,
New versions of the library may add more cases. Always take the path from the exception message.
Ignore All Ignorable Exceptions
Call ExceptionManager.set_ignore_all(True) to ignore every exception that can be ignored. The ExceptionManager.ignore_all property returns the current value and is read-only.
from aspose.email.calendar import Appointment
from aspose.email.exceptions import ExceptionManager
ExceptionManager.set_ignore_all(True)
# The invalid INTERVAL rule part is dropped, the appointment is loaded
appointment = Appointment.load("event.ics")
print(appointment.recurrence) # FREQ=DAILY;COUNT=3
Ignore Selected Exceptions
To ignore only some cases, add their paths from the exception messages to ExceptionManager.ignore_list. An entry also covers every path that starts with it. For example, RecurrenceRule covers all recurrence rule cases.
from aspose.email.calendar import Appointment
from aspose.email.exceptions import ExceptionManager
ExceptionManager.ignore_list.append(r"RecurrenceRule\IncorrectIntervalRulePart")
appointment = Appointment.load("event.ics")
print(appointment.recurrence) # FREQ=DAILY;COUNT=3
IgnoreExceptionsHandler and IgnoreExceptionsLogHandler callbacks of Aspose.Email for .NET are not available in Python. Use set_ignore_all or ignore_list instead.
Restore the Default Behavior
ExceptionManager settings are static. They apply to the whole application until you change them. To raise all exceptions again, reset the settings:
from aspose.email.exceptions import ExceptionManager
ExceptionManager.set_ignore_all(False)
ExceptionManager.ignore_list.clear()