[PATCH 4/4] folder/Base: Convert exceptions to self.ui.error()

Sebastian Spaeth Sebastian at SSpaeth.de
Tue Jun 14 09:23:42 BST 2011


Use ui.error() to output error to log and queue them until the sync
finishes. Check in more places for OfflineImapErrors, bubble them up if
they are severe, and output them otherwise.

Signed-off-by: Sebastian Spaeth <Sebastian at SSpaeth.de>
---
 offlineimap/folder/Base.py |  140 +++++++++++++++++++++++---------------------
 1 files changed, 74 insertions(+), 66 deletions(-)

diff --git a/offlineimap/folder/Base.py b/offlineimap/folder/Base.py
index 608d361..3dbee8c 100644
--- a/offlineimap/folder/Base.py
+++ b/offlineimap/folder/Base.py
@@ -19,6 +19,7 @@ from offlineimap import threadutil
 from offlineimap.ui import getglobalui
 import os.path
 import re
+import sys
 import traceback
 
 class BaseFolder(object):
@@ -229,54 +230,47 @@ class BaseFolder(object):
         # synced to the status cache.  This is only a problem with
         # self.getmessage().  So, don't call self.getmessage unless
         # really needed.
-        try:
-            if register: # output that we start a new thread
-                self.ui.registerthread(self.getaccountname())
-
-            message = None
-            flags = self.getmessageflags(uid)
-            rtime = self.getmessagetime(uid)
-
-            if uid > 0 and dstfolder.uidexists(uid):
-                # dst has message with that UID already, only update status
-                statusfolder.savemessage(uid, None, flags, rtime)
-                return
-
-            self.ui.copyingmessage(uid, self, [dstfolder])
-            # If any of the destinations actually stores the message body,
-            # load it up.
-            if dstfolder.storesmessages():
-
-                message = self.getmessage(uid)
-            #Succeeded? -> IMAP actually assigned a UID. If newid
-            #remained negative, no server was willing to assign us an
-            #UID. If newid is 0, saving succeeded, but we could not
-            #retrieve the new UID. Ignore message in this case.
-            newuid = dstfolder.savemessage(uid, message, flags, rtime)
-            if newuid > 0:
-                if newuid != uid:
-                    # Got new UID, change the local uid.
-                    #TODO: Maildir could do this with a rename rather than
-                    #load/save/del operation, IMPLEMENT a changeuid()
-                    #function or so.
-                    self.savemessage(newuid, message, flags, rtime)
-                    self.deletemessage(uid)
-                    uid = newuid
-                # Save uploaded status in the statusfolder
-                statusfolder.savemessage(uid, message, flags, rtime)
-            else:
-                raise UserWarning("Trying to save msg (uid %d) on folder "
-                                  "%s returned invalid uid %d" % \
-                                      (uid,
-                                       dstfolder.getvisiblename(),
-                                       newuid))
-        except (KeyboardInterrupt):
-            raise
-        except:
-            self.ui.warn("ERROR attempting to copy message " + str(uid) \
-                 + " for account " + self.getaccountname() + ":" \
-                 + traceback.format_exc())
-            raise
+
+        if register: # output that we start a new thread
+            self.ui.registerthread(self.getaccountname())
+
+        message = None
+        flags = self.getmessageflags(uid)
+        rtime = self.getmessagetime(uid)
+
+        if uid > 0 and dstfolder.uidexists(uid):
+            # dst has message with that UID already, only update status
+            statusfolder.savemessage(uid, None, flags, rtime)
+            return
+
+        self.ui.copyingmessage(uid, self, [dstfolder])
+        # If any of the destinations actually stores the message body,
+        # load it up.
+        if dstfolder.storesmessages():
+
+            message = self.getmessage(uid)
+        #Succeeded? -> IMAP actually assigned a UID. If newid
+        #remained negative, no server was willing to assign us an
+        #UID. If newid is 0, saving succeeded, but we could not
+        #retrieve the new UID. Ignore message in this case.
+        newuid = dstfolder.savemessage(uid, message, flags, rtime)
+        if newuid > 0:
+            if newuid != uid:
+                # Got new UID, change the local uid.
+                #TODO: Maildir could do this with a rename rather than
+                #load/save/del operation, IMPLEMENT a changeuid()
+                #function or so.
+                self.savemessage(newuid, message, flags, rtime)
+                self.deletemessage(uid)
+                uid = newuid
+            # Save uploaded status in the statusfolder
+            statusfolder.savemessage(uid, message, flags, rtime)
+        else:
+            raise UserWarning("Trying to save msg (uid %d) on folder "
+                              "%s returned invalid uid %d" % \
+                                  (uid,
+                                   dstfolder.getvisiblename(),
+                                   newuid))
 
     def syncmessagesto_copy(self, dstfolder, statusfolder):
         """Pass1: Copy locally existing messages not on the other side
@@ -295,20 +289,30 @@ class BaseFolder(object):
                               statusfolder.uidexists(uid),
                             self.getmessageuidlist())
         for uid in copylist:
-            if self.suggeststhreads():
-                self.waitforthread()
-                thread = threadutil.InstanceLimitedThread(\
-                    self.getcopyinstancelimit(),
-                    target = self.copymessageto,
-                    name = "Copy message %d from %s" % (uid,
+            try:
+                if self.suggeststhreads():
+                    self.waitforthread()
+                    thread = threadutil.InstanceLimitedThread(\
+                        self.getcopyinstancelimit(),
+                        target = self.copymessageto,
+                        name = "Copy message %d from %s" % (uid,
                                                         self.getvisiblename()),
-                    args = (uid, dstfolder, statusfolder))
-                thread.setDaemon(1)
-                thread.start()
-                threads.append(thread)
-            else:
-                self.copymessageto(uid, dstfolder, statusfolder, register = 0)
-
+                        args = (uid, dstfolder, statusfolder))
+                    thread.setDaemon(1)
+                    thread.start()
+                    threads.append(thread)
+                else:
+                    self.copymessageto(uid, dstfolder, statusfolder,
+                                       register = 0)
+            except OfflineImapError, e:
+                if e.severity > OfflineImapError.ERROR.Message:
+                    raise # buble severe errors up
+                self.ui.error(e, sys.exc_traceback)
+            except Exception, e:
+                self.ui.error(e, "Copying message %s [acc: %s]:\n %s" %\
+                                  (uid, self.getaccountname(),
+                                   traceback.format_exc()))
+                raise    #raise on unknown errors, so we can fix those
         for thread in threads:
             thread.join()
 
@@ -413,8 +417,12 @@ class BaseFolder(object):
                 action(dstfolder, statusfolder)
             except (KeyboardInterrupt):
                 raise
-            except:
-                self.ui.warn("ERROR attempting to sync flags " \
-                             + "for account " + self.getaccountname() \
-                             + ":" + traceback.format_exc())
-                raise
+            except OfflineImap, e:
+                if e.severity > OfflineImapError.ERROR.FOLDER:
+                    raise
+                self.ui.error(e, sys.exc_traceback)
+            except Exception, e:
+                self.ui.error(e, msg = "ERROR attempting to sync folder %s "
+                             "[acc: %s]:\n  %s" (self, self.getaccountname(),
+                                                 traceback.format_exc()))
+                raise # raise unknown Exceptions so we can fix them
-- 
1.7.4.1





More information about the OfflineIMAP-project mailing list