From 103524c97970c3f4aaeefcf86f73f91dc0924629 Mon Sep 17 00:00:00 2001 From: Sebastian Spaeth Date: Fri, 31 Aug 2012 20:05:01 +0200 Subject: [PATCH] Throw OfflineImapErrors rather than weird Exceptions When misconfiguring OLI, e.g. by specifying a repository name that was not configured anywhere, we would bomb out with cryptic "NoSectionError". Throw OfflineImapError that explains what has happened. We still need to avoid throwing exceptions with Tracebacks here though. Signed-off-by: Sebastian Spaeth --- Changelog.rst | 1 + offlineimap/repository/__init__.py | 26 +++++++++++++++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/Changelog.rst b/Changelog.rst index f126075..0a57919 100644 --- a/Changelog.rst +++ b/Changelog.rst @@ -17,6 +17,7 @@ WIP (add new stuff for the next release) necessary, it might have been breaking things. (J. Wiegley) * Improve regex that could lead to 'NoneType' object has no attribute 'group' (D. Franke) +* Improved error throwing on repository misconfiguration OfflineIMAP v6.5.4 (2012-06-02) =============================== diff --git a/offlineimap/repository/__init__.py b/offlineimap/repository/__init__.py index 0059bdf..22cd128 100644 --- a/offlineimap/repository/__init__.py +++ b/offlineimap/repository/__init__.py @@ -15,10 +15,17 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +try: + from configparser import NoSectionError +except ImportError: #python2 + from ConfigParser import NoSectionError + from offlineimap.repository.IMAP import IMAPRepository, MappedIMAPRepository from offlineimap.repository.Gmail import GmailRepository from offlineimap.repository.Maildir import MaildirRepository from offlineimap.repository.LocalStatus import LocalStatusRepository +from offlineimap.error import OfflineImapError + class Repository(object): """Abstract class that returns the correct Repository type @@ -47,17 +54,26 @@ class Repository(object): return LocalStatusRepository(name, account) else: - raise ValueError("Request type %s not supported" % reqtype) + errstr = "Repository type %s not supported" % reqtype + raise OfflineImapError(errstr, OfflineImapError.ERROR.REPO) + # Get repository type config = account.getconfig() - repostype = config.get('Repository ' + name, 'type').strip() + try: + repostype = config.get('Repository ' + name, 'type').strip() + except NoSectionError as e: + errstr = ("Could not find section '%s' in configuration. Required " + "for account '%s'." % ('Repository %s' % name, account)) + raise OfflineImapError(errstr, OfflineImapError.ERROR.REPO) + try: repo = typemap[repostype] except KeyError: - raise ValueError("'%s' repository not supported for %s repositories" - "." % (repostype, reqtype)) - return repo(name, account) + errstr = "'%s' repository not supported for '%s' repositories." \ + % (repostype, reqtype) + raise OfflineImapError(errstr, OfflineImapError.ERROR.REPO) + return repo(name, account) def __init__(self, account, reqtype): """Load the correct Repository type and return that. The