From 33029403821a1e862786e559bb04cfb9b97f59d4 Mon Sep 17 00:00:00 2001 From: Sebastian Spaeth Date: Thu, 18 Aug 2011 09:08:56 +0200 Subject: [PATCH] Proper error handling for SEARCH and FETCH failures from the server SEARCH and FETCH were never checking that the IMAP server actually returned OK. Throw OfflineImapErrors at severity FOLDER in case one of them fails. Signed-off-by: Sebastian Spaeth Signed-off-by: Nicolas Sebrecht --- offlineimap/folder/IMAP.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/offlineimap/folder/IMAP.py b/offlineimap/folder/IMAP.py index 8c42419..417b80a 100644 --- a/offlineimap/folder/IMAP.py +++ b/offlineimap/folder/IMAP.py @@ -144,15 +144,28 @@ class IMAPFolder(BaseFolder): search_cond += ")" res_type, res_data = imapobj.search(None, search_cond) - # Result UIDs seperated by space, coalesce into ranges - messagesToFetch = imaputil.uid_sequence(res_data.split()) - if not messagesToFetch: + if res_type != 'OK': + raise OfflineImapError("SEARCH in folder [%s]%s failed. " + "Search string was '%s'. Server responded '[%s] %s'" % ( + self.getrepository(), self, + search_cond, res_type, res_data), + OfflineImapError.ERROR.FOLDER) + + # Result UIDs are seperated by space, coalesce into ranges + msgsToFetch = imaputil.uid_sequence(res_data.split()) + if not msgsToFetch: return # No messages to sync # Get the flags and UIDs for these. single-quotes prevent # imaplib2 from quoting the sequence. res_type, response = imapobj.fetch("'%s'" % msgsToFetch, '(FLAGS UID)') + if res_type != 'OK': + raise OfflineImapError("FETCHING UIDs in folder [%s]%s failed. " + "Server responded '[%s] %s'" % ( + self.getrepository(), self, + res_type, response), + OfflineImapError.ERROR.FOLDER) finally: self.imapserver.releaseconnection(imapobj)