offlineimap3 icon indicating copy to clipboard operation
offlineimap3 copied to clipboard

Skip parsing date after failing to parse the date

Open me-and opened this issue 1 year ago • 0 comments

This PR

  • [x] I've read the DCO.
  • [x] I've read the Coding Guidelines
  • [x] The relevant informations about the changes stands in the commit message, not here in the message of the pull request.
  • [x] Code changes follow the style of the files they change.
  • [x] Code is tested (provide details).

References

  • Issue #134

Additional information

When file_use_mail_timestamp or utime_from_header are enabled, OfflineIMAP tries to parse the Date header in the email. If the header is present but invalid -- it doesn't contain a valid date -- this will cause email.message to raise an exception. This is all fine. However when handling that exception, OfflineIMAP can't try to extract the date again: it's clearly invalid, and raising the same exception a second time while handling the first exception just causes the entire sync to fail.

To avoid that happening, don't try to provide the invalid date string in the error message. Instead, just give the user the UID of the email that triggered the exception, and the exception text.

Ideally we'd instead fix the code to actually extract the header value and provide it in the error message, but Python's email.message module doesn't provide an easy way to get the raw text of the Date header from an EmailMessage object; it's possible using private variables like EmailMessage._headers, or by parsing the email using a custom email.policy.EmailPolicy object that disables the module's attempts to coerce the header value to a DateTime. However, a user should be able to get the problematic Date header from the message directly anyway, so it's not worth adding all that complexity for something that should be rare and provides little value.

I've tested it against the mail sync that led to me raising #134.

me-and avatar Aug 25 '22 15:08 me-and