]> git.notmuchmail.org Git - notmuch/blobdiff - lib/message.cc
lib: catch exceptions in n_m_get_filenames
[notmuch] / lib / message.cc
index 5c9b58b22986cd9fff286e3493dffedc2ade7450..8e43a393c55171b425175537298c0ff0fecb6c3b 100644 (file)
@@ -90,6 +90,18 @@ _notmuch_message_destructor (notmuch_message_t *message)
     return 0;
 }
 
+#define LOG_XAPIAN_EXCEPTION(message, error) _log_xapian_exception (__location__, message, error)
+
+static void
+_log_xapian_exception (const char *where, notmuch_message_t *message,  const Xapian::Error error) {
+    notmuch_database_t *notmuch = notmuch_message_get_database (message);
+    _notmuch_database_log (notmuch,
+                          "A Xapian exception occurred %s retrieving %s : %s\n",
+                          where,
+                          error.get_msg ().c_str ());
+    notmuch->exception_reported = true;
+}
+
 static notmuch_message_t *
 _notmuch_message_create_for_document (const void *talloc_owner,
                                      notmuch_database_t *notmuch,
@@ -447,9 +459,6 @@ _notmuch_message_ensure_metadata (notmuch_message_t *message, void *field)
            if (status != NOTMUCH_STATUS_SUCCESS)
                INTERNAL_ERROR ("unhandled error from notmuch_database_reopen: %s\n",
                                notmuch_status_to_string (status));
-       } catch (const Xapian::Error &error) {
-           INTERNAL_ERROR ("A Xapian exception occurred fetching message metadata: %s\n",
-                           error.get_msg ().c_str ());
        }
     }
     message->last_view = message->notmuch->view;
@@ -507,7 +516,13 @@ _notmuch_message_get_doc_id (notmuch_message_t *message)
 const char *
 notmuch_message_get_message_id (notmuch_message_t *message)
 {
-    _notmuch_message_ensure_metadata (message, message->message_id);
+    try {
+       _notmuch_message_ensure_metadata (message, message->message_id);
+    } catch (const Xapian::Error &error) {
+       LOG_XAPIAN_EXCEPTION (message, error);
+       return NULL;
+    }
+
     if (! message->message_id)
        INTERNAL_ERROR ("Message with document ID of %u has no message ID.\n",
                        message->doc_id);
@@ -557,9 +572,7 @@ notmuch_message_get_header (notmuch_message_t *message, const char *header)
                return talloc_strdup (message, value.c_str ());
 
        } catch (Xapian::Error &error) {
-           _notmuch_database_log (notmuch_message_get_database (message), "A Xapian exception occurred when reading header: %s\n",
-                                  error.get_msg ().c_str ());
-           message->notmuch->exception_reported = true;
+           LOG_XAPIAN_EXCEPTION (message, error);
            return NULL;
        }
     }
@@ -589,7 +602,12 @@ _notmuch_message_get_in_reply_to (notmuch_message_t *message)
 const char *
 notmuch_message_get_thread_id (notmuch_message_t *message)
 {
-    _notmuch_message_ensure_metadata (message, message->thread_id);
+    try {
+       _notmuch_message_ensure_metadata (message, message->thread_id);
+    } catch (Xapian::Error &error) {
+       LOG_XAPIAN_EXCEPTION (message, error);
+       return NULL;
+    }
     if (! message->thread_id)
        INTERNAL_ERROR ("Message with document ID of %u has no thread ID.\n",
                        message->doc_id);
@@ -751,9 +769,9 @@ _notmuch_message_remove_indexed_terms (notmuch_message_t *message)
 
        const char *tag = notmuch_tags_get (tags);
 
-       if (STRNCMP_LITERAL (tag, "encrypted") != 0 &&
-           STRNCMP_LITERAL (tag, "signed") != 0 &&
-           STRNCMP_LITERAL (tag, "attachment") != 0) {
+       if (strcmp (tag, "encrypted") != 0 &&
+           strcmp (tag, "signed") != 0 &&
+           strcmp (tag, "attachment") != 0) {
            std::string term = tag_prefix + tag;
            message->doc.add_term (term);
        }
@@ -1104,7 +1122,12 @@ _notmuch_message_ensure_filename_list (notmuch_message_t *message)
 const char *
 notmuch_message_get_filename (notmuch_message_t *message)
 {
-    _notmuch_message_ensure_filename_list (message);
+    try {
+       _notmuch_message_ensure_filename_list (message);
+    } catch (Xapian::Error &error) {
+       LOG_XAPIAN_EXCEPTION (message, error);
+       return NULL;
+    }
 
     if (message->filename_list == NULL)
        return NULL;
@@ -1120,7 +1143,12 @@ notmuch_message_get_filename (notmuch_message_t *message)
 notmuch_filenames_t *
 notmuch_message_get_filenames (notmuch_message_t *message)
 {
-    _notmuch_message_ensure_filename_list (message);
+    try {
+       _notmuch_message_ensure_filename_list (message);
+    } catch (Xapian::Error &error) {
+       LOG_XAPIAN_EXCEPTION (message, error);
+       return NULL;
+    }
 
     return _notmuch_filenames_create (message, message->filename_list);
 }