correctly reraise errors with six

Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
This commit is contained in:
Nicolas Sebrecht
2016-06-29 03:42:57 +02:00
parent 69c0080323
commit 7945e10a76
13 changed files with 139 additions and 86 deletions

View File

@ -139,10 +139,14 @@ class GmailFolder(IMAPFolder):
res_type, response = imapobj.fetch("'%s'"% msgsToFetch,
'(FLAGS X-GM-LABELS UID)')
if res_type != 'OK':
six.reraise(OfflineImapError("FETCHING UIDs in folder [%s]%s failed. " % \
(self.getrepository(), self) + \
"Server responded '[%s] %s'" % \
(res_type, response), OfflineImapError.ERROR.FOLDER), None, exc_info()[2])
six.reraise(OfflineImapError,
OfflineImapError(
"FETCHING UIDs in folder [%s]%s failed. "%
(self.getrepository(), self) +
"Server responded '[%s] %s'"%
(res_type, response),
OfflineImapError.ERROR.FOLDER),
exc_info()[2])
finally:
self.imapserver.releaseconnection(imapobj)

View File

@ -1,5 +1,5 @@
# Maildir folder support with labels
# Copyright (C) 2002 - 2016 John Goerzen & contributors
# Copyright (C) 2002-2016 John Goerzen & contributors.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@ -176,8 +176,11 @@ class GmailMaildirFolder(MaildirFolder):
try:
os.rename(tmppath, filepath)
except OSError as e:
six.reraise(OfflineImapError("Can't rename file '%s' to '%s': %s" % \
(tmppath, filepath, e[1]), OfflineImapError.ERROR.FOLDER), None, exc_info()[2])
six.reraise(OfflineImapError,
OfflineImapError("Can't rename file '%s' to '%s': %s"%
(tmppath, filepath, e[1]),
OfflineImapError.ERROR.FOLDER),
exc_info()[2])
# if utime_from_header=true, we don't want to change the mtime.
if self.utime_from_header and mtime:

View File

@ -1,5 +1,5 @@
# IMAP folder support
# Copyright (C) 2002-2016 John Goerzen & contributors
# Copyright (C) 2002-2016 John Goerzen & contributors.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@ -84,8 +84,11 @@ class IMAPFolder(BaseFolder):
def getmaxage(self):
if self.config.getdefault("Account %s"%
self.accountname, "maxage", None):
six.reraise(OfflineImapError("maxage is not supported on IMAP-IMAP sync",
OfflineImapError.ERROR.REPO), None, exc_info()[2])
six.reraise(OfflineImapError,
OfflineImapError(
"maxage is not supported on IMAP-IMAP sync",
OfflineImapError.ERROR.REPO),
exc_info()[2])
# Interface from BaseFolder
def getinstancelimitnamespace(self):
@ -613,11 +616,13 @@ class IMAPFolder(BaseFolder):
self.imapserver.releaseconnection(imapobj, True)
imapobj = self.imapserver.acquireconnection()
if not retry_left:
six.reraise(OfflineImapError("Saving msg (%s) in folder '%s', "
"repository '%s' failed (abort). Server responded: %s\n"
"Message content was: %s"%
(msg_id, self, self.getrepository(), str(e), dbg_output),
OfflineImapError.ERROR.MESSAGE), None, exc_info()[2])
six.reraise(OfflineImapError,
OfflineImapError("Saving msg (%s) in folder '%s', "
"repository '%s' failed (abort). Server responded: %s\n"
"Message content was: %s"%
(msg_id, self, self.getrepository(), str(e), dbg_output),
OfflineImapError.ERROR.MESSAGE),
exc_info()[2])
# XXX: is this still needed?
self.ui.error(e, exc_info()[2])
except imapobj.error as e: # APPEND failed
@ -626,10 +631,12 @@ class IMAPFolder(BaseFolder):
# drop conn, it might be bad.
self.imapserver.releaseconnection(imapobj, True)
imapobj = None
six.reraise(OfflineImapError("Saving msg (%s) folder '%s', repo '%s'"
"failed (error). Server responded: %s\nMessage content was: "
"%s" % (msg_id, self, self.getrepository(), str(e), dbg_output),
OfflineImapError.ERROR.MESSAGE), None, exc_info()[2])
six.reraise(OfflineImapError,
OfflineImapError("Saving msg (%s) folder '%s', repo '%s'"
"failed (error). Server responded: %s\nMessage content was: "
"%s"% (msg_id, self, self.getrepository(), str(e), dbg_output),
OfflineImapError.ERROR.MESSAGE),
exc_info()[2])
# Checkpoint. Let it write out stuff, etc. Eg searches for
# just uploaded messages won't work if we don't do this.
(typ,dat) = imapobj.check()

View File

@ -1,5 +1,5 @@
# Local status cache virtual folder
# Copyright (C) 2002-2016 John Goerzen & contributors
# Copyright (C) 2002-2016 John Goerzen & contributors.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@ -75,7 +75,7 @@ class LocalStatusFolder(BaseFolder):
errstr = ("Corrupt line '%s' in cache file '%s'"%
(line, self.filename))
self.ui.warn(errstr)
six.reraise(ValueError(errstr), None, exc_info()[2])
six.reraise(ValueError, ValueError(errstr), exc_info()[2])
self.messagelist[uid] = self.msglist_item_initializer(uid)
self.messagelist[uid]['flags'] = flags
@ -98,7 +98,7 @@ class LocalStatusFolder(BaseFolder):
errstr = "Corrupt line '%s' in cache file '%s'"% \
(line, self.filename)
self.ui.warn(errstr)
six.reraise(ValueError(errstr), None, exc_info()[2])
six.reraise(ValueError, ValueError(errstr), exc_info()[2])
self.messagelist[uid] = self.msglist_item_initializer(uid)
self.messagelist[uid]['flags'] = flags
self.messagelist[uid]['mtime'] = mtime

View File

@ -1,5 +1,5 @@
# Local status cache virtual folder: SQLite backend
# Copyright (C) 2009-2016 Stewart Smith and contributors
# Copyright (C) 2009-2016 Stewart Smith and contributors.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@ -67,10 +67,13 @@ class LocalStatusSQLiteFolder(BaseFolder):
self.connection = sqlite.connect(self.filename, check_same_thread=False)
except sqlite.OperationalError as e:
# Operation had failed.
six.reraise(UserWarning("cannot open database file '%s': %s.\nYou might "
"want to check the rights to that file and if it cleanly opens "
"with the 'sqlite<3>' command."%
(self.filename, e)), None, exc_info()[2])
six.reraise(UserWarning,
UserWarning(
"cannot open database file '%s': %s.\nYou might "
"want to check the rights to that file and if it "
"cleanly opens with the 'sqlite<3>' command."%
(self.filename, e)),
exc_info()[2])
# Make sure sqlite is in multithreading SERIALIZE mode.
assert sqlite.threadsafety == 1, 'Your sqlite is not multithreading safe.'

View File

@ -1,5 +1,5 @@
# Maildir folder support
# Copyright (C) 2002-2016 John Goerzen & contributors
# Copyright (C) 2002-2016 John Goerzen & contributors.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@ -22,8 +22,6 @@ import os
import six
from sys import exc_info
from threading import Lock
from .Base import BaseFolder
try:
from hashlib import md5
except ImportError:
@ -34,6 +32,7 @@ except NameError:
from sets import Set as set
from offlineimap import OfflineImapError, emailutil
from .Base import BaseFolder
# Find the UID in a message filename
re_uidmatch = re.compile(',U=(\d+)')
@ -318,8 +317,11 @@ class MaildirFolder(BaseFolder):
time.sleep(0.23)
continue
severity = OfflineImapError.ERROR.MESSAGE
six.reraise(OfflineImapError("Unique filename %s already exists."%
filename, severity), None, exc_info()[2])
six.reraise(OfflineImapError,
OfflineImapError(
"Unique filename %s already exists."%
filename, severity),
exc_info()[2])
else:
raise
@ -439,9 +441,12 @@ class MaildirFolder(BaseFolder):
os.rename(os.path.join(self.getfullname(), oldfilename),
os.path.join(self.getfullname(), newfilename))
except OSError as e:
six.reraise(OfflineImapError("Can't rename file '%s' to '%s': %s" % (
oldfilename, newfilename, e[1]),
OfflineImapError.ERROR.FOLDER), None, exc_info()[2])
six.reraise(OfflineImapError,
OfflineImapError(
"Can't rename file '%s' to '%s': %s"%
(oldfilename, newfilename, e[1]),
OfflineImapError.ERROR.FOLDER),
exc_info()[2])
self.messagelist[uid]['flags'] = flags
self.messagelist[uid]['filename'] = newfilename
@ -519,10 +524,12 @@ class MaildirFolder(BaseFolder):
try:
os.rename(filename, newfilename)
except OSError as e:
six.reraise(OfflineImapError(
"Can't rename file '%s' to '%s': %s" % (
filename, newfilename, e[1]),
OfflineImapError.ERROR.FOLDER), None, exc_info()[2])
six.reraise(OfflineImapError,
OfflineImapError(
"Can't rename file '%s' to '%s': %s"%
(filename, newfilename, e[1]),
OfflineImapError.ERROR.FOLDER),
exc_info()[2])
elif match.group(1) != self._foldermd5:
self.ui.warn(("Inconsistent FMD5 for file `%s':"
" Neither `%s' nor `%s' found")

View File

@ -1,5 +1,5 @@
# Base folder support
# Copyright (C) 2002-2015 John Goerzen & contributors
# Copyright (C) 2002-2016 John Goerzen & contributors.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@ -15,13 +15,14 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
from sys import exc_info
import six
from threading import Lock
from offlineimap import OfflineImapError
from .IMAP import IMAPFolder
from sys import exc_info
import os.path
import six
from offlineimap import OfflineImapError
from .IMAP import IMAPFolder
class MappedIMAPFolder(IMAPFolder):
"""IMAP class to map between Folder() instances where both side assign a uid
@ -63,8 +64,11 @@ class MappedIMAPFolder(IMAPFolder):
try:
line = line.strip()
except ValueError:
six.reraise(Exception("Corrupt line '%s' in UID mapping file '%s'"%
(line, mapfilename)), None, exc_info()[2])
six.reraise(Exception,
Exception(
"Corrupt line '%s' in UID mapping file '%s'"%
(line, mapfilename)),
exc_info()[2])
(str1, str2) = line.split(':')
loc = int(str1)
rem = int(str2)
@ -90,10 +94,14 @@ class MappedIMAPFolder(IMAPFolder):
try:
return [mapping[x] for x in items]
except KeyError as e:
six.reraise(OfflineImapError("Could not find UID for msg '{0}' (f:'{1}'."
" This is usually a bad thing and should be reported on the ma"
"iling list.".format(e.args[0], self),
OfflineImapError.ERROR.MESSAGE), None, exc_info()[2])
six.reraise(OfflineImapError,
OfflineImapError(
"Could not find UID for msg '{0}' (f:'{1}'."
" This is usually a bad thing and should be "
"reported on the mailing list.".format(
e.args[0], self),
OfflineImapError.ERROR.MESSAGE),
exc_info()[2])
# Interface from BaseFolder
def cachemessagelist(self, min_date=None, min_uid=None):