[PATCH 7/9] python: provide more exception classes

Justus Winter 4winter at informatik.uni-hamburg.de
Sun Sep 25 18:05:35 PDT 2011


To make the exception handling more effective in code using the
python bindings it is necessary to differentiate between the
different kind of failures.

Add an exception class for each status code and add a decode
classmethod to the NotmuchError class that acts as a factory.

Import the new classes in __init__.py so they can be easily
imported by anyone.

Signed-off-by: Justus Winter <4winter at informatik.uni-hamburg.de>
---
 bindings/python/notmuch/__init__.py |   16 ++++++++++++++-
 bindings/python/notmuch/globals.py  |   37 +++++++++++++++++++++++++++++++++++
 2 files changed, 52 insertions(+), 1 deletions(-)

diff --git a/bindings/python/notmuch/__init__.py b/bindings/python/notmuch/__init__.py
index a7b558f..7e6a68c 100644
--- a/bindings/python/notmuch/__init__.py
+++ b/bindings/python/notmuch/__init__.py
@@ -56,7 +56,21 @@ from notmuch.database import Database, Query
 from notmuch.message import Messages, Message
 from notmuch.thread import Threads, Thread
 from notmuch.tag import Tags
-from notmuch.globals import nmlib, STATUS, NotmuchError
+from notmuch.globals import (
+    nmlib,
+    STATUS,
+    NotmuchError,
+    OutOfMemoryError,
+    ReadOnlyDatabaseError,
+    XapianError,
+    FileError,
+    FileNotEmailError,
+    DuplicateMessageIdError,
+    NullPointerError,
+    TagTooLongError,
+    UnbalancedFreezeThawError,
+    NotInitializedError
+)
 from notmuch.version import __VERSION__
 __LICENSE__ = "GPL v3+"
 __AUTHOR__ = 'Sebastian Spaeth <Sebastian at SSpaeth.de>'
diff --git a/bindings/python/notmuch/globals.py b/bindings/python/notmuch/globals.py
index 8b73f91..e454384 100644
--- a/bindings/python/notmuch/globals.py
+++ b/bindings/python/notmuch/globals.py
@@ -102,6 +102,43 @@ class NotmuchError(Exception):
         else:
             return 'Unknown error'
 
+    @classmethod
+    def decode(cls, status, message=None):
+        assert 0 < status <= 10
+        return [
+            OutOfMemoryError,
+            ReadOnlyDatabaseError,
+            XapianError,
+            FileError,
+            FileNotEmailError,
+            DuplicateMessageIdError,
+            NullPointerError,
+            TagTooLongError,
+            UnbalancedFreezeThawError,
+            NotInitializedError
+        ][status - 1](message)
+
+class OutOfMemoryError(NotmuchError):
+    status = 1
+class ReadOnlyDatabaseError(NotmuchError):
+    status = 2
+class XapianError(NotmuchError):
+    status = 3
+class FileError(NotmuchError):
+    status = 4
+class FileNotEmailError(NotmuchError):
+    status = 5
+class DuplicateMessageIdError(NotmuchError):
+    status = 6
+class NullPointerError(NotmuchError):
+    status = 7
+class TagTooLongError(NotmuchError):
+    status = 8
+class UnbalancedFreezeThawError(NotmuchError):
+    status = 9
+class NotInitializedError(NotmuchError):
+    status = 10
+
 def _str(value):
     """Ensure a nicely utf-8 encoded string to pass to libnotmuch
 
-- 
1.7.6.3



More information about the notmuch mailing list