unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: David Bremner <david@tethera.net>
To: notmuch@notmuchmail.org
Cc: David Bremner <david@tethera.net>
Subject: [PATCH 4/4] lib/n_d_get_version: catch exceptions and clarify the API
Date: Tue, 14 Jul 2020 19:41:19 -0300	[thread overview]
Message-ID: <20200714224119.717845-5-david@tethera.net> (raw)
In-Reply-To: <20200714224119.717845-1-david@tethera.net>

notmuch_database_get_version previously returned 0 on some errors, but
did not document this. Luckily 0 is not a valid database version.
---
 lib/database.cc           | 19 ++++++++++++++++++-
 lib/notmuch.h             |  3 +++
 test/T562-lib-database.sh |  1 -
 3 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/lib/database.cc b/lib/database.cc
index cfb19ccb..27861970 100644
--- a/lib/database.cc
+++ b/lib/database.cc
@@ -58,6 +58,17 @@ typedef struct {
 #define DB_ACTION Xapian::DB_CREATE_OR_OPEN
 #endif
 
+#define LOG_XAPIAN_EXCEPTION(message, error) _log_xapian_exception (__location__, message, error)
+
+static void
+_log_xapian_exception (const char *where, notmuch_database_t *notmuch,  const Xapian::Error error) {
+    _notmuch_database_log (notmuch,
+			   "A Xapian exception occurred %s accessing %s : %s\n",
+			   where,
+			   error.get_msg ().c_str ());
+    notmuch->exception_reported = true;
+}
+
 /* Here's the current schema for our database (for NOTMUCH_DATABASE_VERSION):
  *
  * We currently have three different types of documents (mail, ghost,
@@ -1360,7 +1371,13 @@ notmuch_database_get_version (notmuch_database_t *notmuch)
     const char *str;
     char *end;
 
-    version_string = notmuch->xapian_db->get_metadata ("version");
+    try {
+	version_string = notmuch->xapian_db->get_metadata ("version");
+    } catch (const Xapian::Error &error) {
+	LOG_XAPIAN_EXCEPTION (notmuch, error);
+	return 0;
+    }
+
     if (version_string.empty ())
 	return 0;
 
diff --git a/lib/notmuch.h b/lib/notmuch.h
index 97ebc17d..7ee0507a 100644
--- a/lib/notmuch.h
+++ b/lib/notmuch.h
@@ -431,6 +431,8 @@ notmuch_database_get_path (notmuch_database_t *database);
 
 /**
  * Return the database format version of the given database.
+ *
+ * @retval 0 on error
  */
 unsigned int
 notmuch_database_get_version (notmuch_database_t *database);
@@ -444,6 +446,7 @@ notmuch_database_get_version (notmuch_database_t *database);
  * fail with NOTMUCH_STATUS_UPGRADE_REQUIRED.  This always returns
  * FALSE for a read-only database because there's no way to upgrade a
  * read-only database.
+ *
  */
 notmuch_bool_t
 notmuch_database_needs_upgrade (notmuch_database_t *database);
diff --git a/test/T562-lib-database.sh b/test/T562-lib-database.sh
index b8fba7d6..c9705b13 100755
--- a/test/T562-lib-database.sh
+++ b/test/T562-lib-database.sh
@@ -68,7 +68,6 @@ EOF
 test_expect_equal_file EXPECTED OUTPUT
 
 test_begin_subtest "get version with closed db"
-test_subtest_known_broken
 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
     {
         unsigned int version;
-- 
2.27.0

  parent reply	other threads:[~2020-07-14 22:42 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-14 22:41 batch 5, API exception handling cleanup David Bremner
2020-07-14 22:41 ` [PATCH 1/4] test: regression tests for n_d_status_string and n_d_get_path David Bremner
2020-07-14 22:41 ` [PATCH 2/4] test: add known broken test for n_d_get_version on closed db David Bremner
2020-07-14 22:41 ` [PATCH 3/4] lib: move deallocation of memory from n_d_close to n_d_destroy David Bremner
2020-07-14 22:41 ` David Bremner [this message]
2020-07-22 12:15   ` [PATCH 4/4] lib/n_d_get_version: catch exceptions and clarify the API David Bremner
2020-07-23 10:12     ` David Bremner

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=20200714224119.717845-5-david@tethera.net \
    --to=david@tethera.net \
    --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).