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 <Sebastian@SSpaeth.de>
This commit is contained in:
Sebastian Spaeth 2012-08-31 20:05:01 +02:00
parent 925a5bcae1
commit 103524c979
2 changed files with 22 additions and 5 deletions

View File

@ -17,6 +17,7 @@ WIP (add new stuff for the next release)
necessary, it might have been breaking things. (J. Wiegley) necessary, it might have been breaking things. (J. Wiegley)
* Improve regex that could lead to 'NoneType' object has no attribute 'group' * Improve regex that could lead to 'NoneType' object has no attribute 'group'
(D. Franke) (D. Franke)
* Improved error throwing on repository misconfiguration
OfflineIMAP v6.5.4 (2012-06-02) OfflineIMAP v6.5.4 (2012-06-02)
=============================== ===============================

View File

@ -15,10 +15,17 @@
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # 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.IMAP import IMAPRepository, MappedIMAPRepository
from offlineimap.repository.Gmail import GmailRepository from offlineimap.repository.Gmail import GmailRepository
from offlineimap.repository.Maildir import MaildirRepository from offlineimap.repository.Maildir import MaildirRepository
from offlineimap.repository.LocalStatus import LocalStatusRepository from offlineimap.repository.LocalStatus import LocalStatusRepository
from offlineimap.error import OfflineImapError
class Repository(object): class Repository(object):
"""Abstract class that returns the correct Repository type """Abstract class that returns the correct Repository type
@ -47,17 +54,26 @@ class Repository(object):
return LocalStatusRepository(name, account) return LocalStatusRepository(name, account)
else: 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() 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: try:
repo = typemap[repostype] repo = typemap[repostype]
except KeyError: except KeyError:
raise ValueError("'%s' repository not supported for %s repositories" errstr = "'%s' repository not supported for '%s' repositories." \
"." % (repostype, reqtype)) % (repostype, reqtype)
return repo(name, account) raise OfflineImapError(errstr, OfflineImapError.ERROR.REPO)
return repo(name, account)
def __init__(self, account, reqtype): def __init__(self, account, reqtype):
"""Load the correct Repository type and return that. The """Load the correct Repository type and return that. The