From d7ee1489121b62be571484c45ebf7a77d09d04f7 Mon Sep 17 00:00:00 2001 From: Patrick Ulbrich Date: Sat, 18 Apr 2015 16:44:23 +0200 Subject: [PATCH] Don't suppress exceptions occuring in Account.get_connection() --- Mailnag/common/accounts.py | 10 ++++------ Mailnag/daemon/idlers.py | 21 +++++++-------------- Mailnag/daemon/mails.py | 10 +++++++--- 3 files changed, 18 insertions(+), 23 deletions(-) diff --git a/Mailnag/common/accounts.py b/Mailnag/common/accounts.py index 741e9d0..1b3fe6e 100644 --- a/Mailnag/common/accounts.py +++ b/Mailnag/common/accounts.py @@ -123,13 +123,12 @@ class Account: self._conn = conn except: - logging.error("Cannot connect to IMAP account '%s'." % self.server) try: if conn != None: # conn.close() # allowed in SELECTED state only conn.logout() - except: - pass + except: pass + raise # re-throw exception return self._conn @@ -164,12 +163,11 @@ class Account: self._conn = conn except: - logging.error("Cannot connect to POP account: '%s'." % self.server) try: if conn != None: conn.quit() - except: - pass + except: pass + raise # re-throw exception return self._conn diff --git a/Mailnag/daemon/idlers.py b/Mailnag/daemon/idlers.py index 7614255..8237ab7 100644 --- a/Mailnag/daemon/idlers.py +++ b/Mailnag/daemon/idlers.py @@ -29,9 +29,6 @@ from Mailnag.common.imaplib2 import AUTH from Mailnag.common.exceptions import InvalidOperationException -class ConnectionException(Exception): - def __init__(self, message): - Exception.__init__(self, message) # # Idler class # @@ -49,10 +46,6 @@ class Idler(object): self._conn = account.get_connection(use_existing = True) self._disposed = False - if self._conn == None: - raise ConnectionException( - "Failed to establish a connection for account '%s'" % account.name) - # Need to get out of AUTH mode of fresh connections. if self._conn.state == AUTH: self._select(self._conn, account) @@ -136,15 +129,15 @@ class Idler(object): while (self._conn == None) and (not self._event.isSet()): logging.info("Trying to reconnect Idler thread for account '%s'." % self._account.name) - self._conn = self._account.get_connection(use_existing = False) - if self._conn == None: - logging.error("Failed to reconnect Idler thread for account '%s'." % self._account.name) + try: + self._conn = self._account.get_connection(use_existing = False) + logging.info("Successfully reconnected Idler thread for account '%s'." % self._account.name) + except Exception as ex: + logging.error("Failed to reconnect Idler thread for account '%s' (%s)." % (self._account.name, ex)) logging.info("Trying to reconnect Idler thread for account '%s' in %s minutes" % (self._account.name, str(self.RECONNECT_RETRY_INTERVAL))) self._wait(60 * self.RECONNECT_RETRY_INTERVAL) # don't hammer the server - else: - logging.info("Successfully reconnected Idler thread for account '%s'." % self._account.name) - + if self._conn != None: self._select(self._conn, self._account) @@ -181,7 +174,7 @@ class IdlerRunner: idler.start() self._idlerlist.append(idler) except Exception as ex: - logging.error("Error: Failed to create an idler thread for account '%s'" % acc.name) + logging.error("Error: Failed to create an idler thread for account '%s' (%s)" % (acc.name, ex)) def dispose(self): diff --git a/Mailnag/daemon/mails.py b/Mailnag/daemon/mails.py index ecc8f0f..2b85f5b 100644 --- a/Mailnag/daemon/mails.py +++ b/Mailnag/daemon/mails.py @@ -61,10 +61,14 @@ class MailCollector: for acc in self._accounts: # get server connection for this account - conn = acc.get_connection(use_existing = True) - if conn == None: + conn = None + try: + conn = acc.get_connection(use_existing = True) + except Exception as ex: + logging.error("Failed to connect to account '%s' (%s)." % (acc.name, ex)) continue - elif acc.imap: # IMAP + + if acc.imap: # IMAP if len(acc.folders) == 0: folder_list = [ 'INBOX' ] else: