[PATCH 6/6] Proper error handling for SEARCH and FETCH failures from the server

Sebastian Spaeth Sebastian at SSpaeth.de
Mon Aug 15 12:15:59 UTC 2011


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 <Sebastian at SSpaeth.de>
---
 offlineimap/folder/IMAP.py |   19 ++++++++++++++++---
 1 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/offlineimap/folder/IMAP.py b/offlineimap/folder/IMAP.py
index 74cef92..3460ba3 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)
 
-- 
1.7.4.1




More information about the OfflineIMAP-project mailing list