unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Justus Winter <4winter@informatik.uni-hamburg.de>
To: notmuch@notmuchmail.org
Cc: Justus Winter <4winter@informatik.uni-hamburg.de>
Subject: [PATCH 7/9] python: provide more exception classes
Date: Mon, 26 Sep 2011 03:05:35 +0200	[thread overview]
Message-ID: <1316999137-28257-7-git-send-email-4winter@informatik.uni-hamburg.de> (raw)
In-Reply-To: <1316999137-28257-1-git-send-email-4winter@informatik.uni-hamburg.de>

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@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@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

  parent reply	other threads:[~2011-09-26  1:06 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-26  1:05 [PATCH 1/9] python: add a .gitignore file and refine the toplevel one Justus Winter
2011-09-26  1:05 ` [PATCH 2/9] python: add status and message attributes to NotmuchError Justus Winter
2011-09-26  1:05 ` [PATCH 3/9] python: reorder the arguments of NotmuchError.__init__() Justus Winter
2011-09-26  1:05 ` [PATCH 4/9] python: fix NotmuchError.__str__ if status == None Justus Winter
2011-09-26  1:05 ` [PATCH 5/9] python: rename _verify_initialized_db to _assert_db_is_initialized Justus Winter
2011-09-26  1:05 ` [PATCH 6/9] python: rename _verify_dir_initialized to _assert_dir_is_initialized Justus Winter
2011-09-26  1:05 ` Justus Winter [this message]
2011-09-30 12:00   ` [PATCH 7/9] python: provide more exception classes Sebastian Spaeth
2011-09-30 12:23     ` Justus Winter
2011-09-26  1:05 ` [PATCH 8/9] python: use the new exception classes and update the documentation Justus Winter
2011-09-26  1:05 ` [PATCH 9/9] python: raise a more specific error in Messages.print_messages Justus Winter
2011-09-29  7:45 ` [PATCH 1/9] python: add a .gitignore file and refine the toplevel one Sebastian Spaeth
2011-09-30  0:41   ` Justus Winter
2011-09-30  9:14     ` Sebastian Spaeth
2011-09-29  7:47 ` Sebastian Spaeth

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1316999137-28257-7-git-send-email-4winter@informatik.uni-hamburg.de \
    --to=4winter@informatik.uni-hamburg.de \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).