[PATCH 2/5] Throw OfflineImapError on DNS error

Sebastian Spaeth Sebastian at SSpaeth.de
Wed May 4 15:45:25 BST 2011

In case we misconfigured a server name or are otherwise offline, a
socket.gaierror will be raised when attempting to connect. We catch that
case and raise an OfflineImapError with severity ERROR.REPO, meaning we
should stop syncing this account and continue with the next one.

Signed-off-by: Sebastian Spaeth <Sebastian at SSpaeth.de>
 offlineimap/imapserver.py |   24 +++++++++++++++++++-----
 1 files changed, 19 insertions(+), 5 deletions(-)

diff --git a/offlineimap/imapserver.py b/offlineimap/imapserver.py
index c474a01..6e58949 100644
--- a/offlineimap/imapserver.py
+++ b/offlineimap/imapserver.py
@@ -17,7 +17,7 @@
 #    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 from offlineimap import imaplib2 as imaplib
-from offlineimap import imaplibutil, imaputil, threadutil
+from offlineimap import imaplibutil, imaputil, threadutil, OfflineImapError
 from offlineimap.ui import getglobalui
 from threading import *
 import thread
@@ -28,6 +28,7 @@ import base64
 from StringIO import StringIO
 from platform import system
+from socket import gaierror
     # do we have a recent pykerberos?
@@ -271,16 +272,29 @@ class IMAPServer:
             self.lastowner[imapobj] = thread.get_ident()
             return imapobj
-        except:
-            """If we are here then we did not succeed in getting a connection -
-            we should clean up and then re-raise the error..."""
+        except Exception, e:
+            """If we are here then we did not succeed in getting a
+            connection - we should clean up and then re-raise the
+            error..."""
             #Make sure that this can be retried the next time...
             self.passworderror = None
-            raise
+            if type(e) == gaierror:
+                #DNS related errors. Abort Repo sync
+                severity = OfflineImapError.ERROR.REPO
+                #TODO: special error msg for e.errno == 2 "Name or service not known"?
+                reason = "Could not resolve name '%s' for repository "\
+                         "'%s'. Make sure you have configured the ser"\
+                         "ver name correctly and that you are online."\
+                         % (self.hostname, self.reposname)
+                raise OfflineImapError(reason, severity)
+            else:
+                # re-raise all other errors
+                raise
     def connectionwait(self):
         """Waits until there is a connection available.  Note that between

More information about the OfflineIMAP-project mailing list