summaryrefslogtreecommitdiff
path: root/alot
diff options
context:
space:
mode:
authorJohannes Kulick <kulick@hildensia.de>2012-12-19 11:54:56 +0100
committerPatrick Totzke <patricktotzke@gmail.com>2013-02-19 10:10:08 +0000
commit9b8b8f5a68270912ac0dc8f16d320729b25d2da8 (patch)
tree89389307a1afa881c782e5202181c700e1a78fcf /alot
parent591bd897f5473d7ad97f7b2ad37df1aac50386c9 (diff)
add error codes
to avoid import ui stuff to crypto.py I added error codes to the GPGProblem Exceptions. This way I can process them later, depending on the error code
Diffstat (limited to 'alot')
-rw-r--r--alot/commands/envelope.py4
-rw-r--r--alot/crypto.py30
-rw-r--r--alot/errors.py16
3 files changed, 36 insertions, 14 deletions
diff --git a/alot/commands/envelope.py b/alot/commands/envelope.py
index 4c3f1e5f..a4a978f4 100644
--- a/alot/commands/envelope.py
+++ b/alot/commands/envelope.py
@@ -12,7 +12,7 @@ from twisted.internet.defer import inlineCallbacks
import datetime
from alot.account import SendingMailFailed, StoreMailError
-from alot.errors import GPGProblem
+from alot.errors import GPGProblem, GPGCode
from alot import buffers
from alot import commands
from alot import crypto
@@ -484,7 +484,7 @@ class EncryptCommand(Command):
for keyid in self.encrypt_keys:
tmp_key = crypto.get_key(keyid)
del envelope.encrypt_keys[crypto.hash_key(tmp_key)]
- except gpgme.GpgmeError as e:
+ except GPGProblem as e:
ui.notify(e.message, priority='error')
if not envelope.encrypt_keys:
envelope.encrypt = False
diff --git a/alot/crypto.py b/alot/crypto.py
index 97449c78..76d34e8d 100644
--- a/alot/crypto.py
+++ b/alot/crypto.py
@@ -6,7 +6,7 @@ import logging
from email.generator import Generator
from cStringIO import StringIO
-from alot.errors import GPGProblem
+from alot.errors import GPGProblem, GPGCode
from email.mime.multipart import MIMEMultipart
import gpgme
@@ -66,7 +66,8 @@ def _hash_algo_name(hash_algo):
return mapping[hash_algo]
else:
raise GPGProblem(("Invalid hash_algo passed to hash_algo_name."
- " Please report this as a bug in alot."))
+ " Please report this as a bug in alot."),
+ code=GPGCode.INVALID_HASH)
def RFC3156_micalg_from_algo(hash_algo):
@@ -121,11 +122,13 @@ def get_key(keyid):
key = ctx.get_key(keyid)
except gpgme.GpgmeError as e:
if e.code == gpgme.ERR_AMBIGUOUS_NAME:
- # Deferred import to avoid a circular import dependency
- raise GPGProblem(("More than one key found matching this filter."
- " Please be more specific (use a key ID like 4AC8EE1D)."))
+ raise GPGProblem(("More than one key found matching this filter." +
+ " Please be more specific (use a key ID like " +
+ "4AC8EE1D)."),
+ code=GPGCode.AMBIGUOUS_NAME)
elif e.code == gpgme.ERR_INV_VALUE or e.code == gpgme.ERR_EOF:
- raise GPGProblem("Can not find key for " + keyid)
+ raise GPGProblem("Can not find key for \'" + keyid + "\'.",
+ code=GPGCode.NOT_FOUND)
else:
raise e
return key
@@ -202,12 +205,17 @@ def hash_key(key):
def validate_key(key, sign=False, encrypt=False):
if key.revoked:
- raise GPGProblem("The key \"" + key.uids[0].uid + "\" is revoked.")
+ raise GPGProblem("The key \"" + key.uids[0].uid + "\" is revoked.",
+ code=GPGCode.KEY_REVOKED)
elif key.expired:
- raise GPGProblem("The key \"" + key.uids[0].uid + "\" is expired.")
+ raise GPGProblem("The key \"" + key.uids[0].uid + "\" is expired.",
+ code=GPGCode.KEY_EXPIRED)
elif key.invalid:
- raise GPGProblem("The key \"" + key.uids[0].uid + "\" is invalid.")
+ raise GPGProblem("The key \"" + key.uids[0].uid + "\" is invalid.",
+ code=GPGCode.KEY_INVALID)
if encrypt and not key.can_encrypt:
- raise GPGProblem("The key \"" + key.uids[0].uid + "\" can not encrypt.")
+ raise GPGProblem("The key \"" + key.uids[0].uid + "\" can not " +
+ "encrypt.", code=GPGCode.KEY_CANNOT_ENCRYPT)
if sign and not key.can_sign:
- raise GPGProblem("The key \"" + key.uids[0].uid + "\" can not sign.")
+ raise GPGProblem("The key \"" + key.uids[0].uid + "\" can not sign.",
+ code=GPGCode.KEY_CANNOT_SIGN)
diff --git a/alot/errors.py b/alot/errors.py
index 881acf1f..a4169c3c 100644
--- a/alot/errors.py
+++ b/alot/errors.py
@@ -2,7 +2,21 @@
# This file is released under the GNU GPL, version 3 or a later revision.
# For further details see the COPYING file
+class GPGCode:
+ AMBIGUOUS_NAME = 1
+ NOT_FOUND = 2
+ BAD_PASSPHRASE = 3
+ KEY_REVOKED = 4
+ KEY_EXPIRED = 5
+ KEY_INVALID = 6
+ KEY_CANNOT_ENCRYPT = 7
+ KEY_CANNOT_SIGN = 8
+ INVALID_HASHH = 9
+
class GPGProblem(Exception):
"""GPG Error"""
- pass
+ def __init__(self, message, code):
+ self.code = code
+ self.message = message
+ Exception(message)