lib: Audit all notmuch_database call for Xapian exception handling.

Our current approach is for top-level entry poitns in the library
to have try/catch blocks that catch any Xapian exception and print
a message. Add a few missing blocks and fix up the documentation.
This commit is contained in:
Carl Worth 2010-04-24 07:22:34 -07:00
parent 3dbef312fb
commit 9ef68f1444
2 changed files with 77 additions and 34 deletions

View file

@ -341,13 +341,20 @@ notmuch_database_find_message (notmuch_database_t *notmuch,
notmuch_private_status_t status; notmuch_private_status_t status;
unsigned int doc_id; unsigned int doc_id;
status = _notmuch_database_find_unique_doc_id (notmuch, "id", try {
message_id, &doc_id); status = _notmuch_database_find_unique_doc_id (notmuch, "id",
message_id, &doc_id);
if (status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) if (status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND)
return NULL;
return _notmuch_message_create (notmuch, notmuch, doc_id, NULL);
} catch (const Xapian::Error &error) {
fprintf (stderr, "A Xapian exception occurred finding message: %s.\n",
error.get_msg().c_str());
notmuch->exception_reported = TRUE;
return NULL; return NULL;
}
return _notmuch_message_create (notmuch, notmuch, doc_id, NULL);
} }
/* Advance 'str' past any whitespace or RFC 822 comments. A comment is /* Advance 'str' past any whitespace or RFC 822 comments. A comment is
@ -1152,7 +1159,14 @@ notmuch_database_get_directory (notmuch_database_t *notmuch,
{ {
notmuch_status_t status; notmuch_status_t status;
return _notmuch_directory_create (notmuch, path, &status); try {
return _notmuch_directory_create (notmuch, path, &status);
} catch (const Xapian::Error &error) {
fprintf (stderr, "A Xapian exception occurred getting directory: %s.\n",
error.get_msg().c_str());
notmuch->exception_reported = TRUE;
return NULL;
}
} }
static const char * static const char *
@ -1591,7 +1605,7 @@ notmuch_database_add_message (notmuch_database_t *notmuch,
_notmuch_message_sync (message); _notmuch_message_sync (message);
} catch (const Xapian::Error &error) { } catch (const Xapian::Error &error) {
fprintf (stderr, "A Xapian exception occurred adding message: %s.\n", fprintf (stderr, "A Xapian exception occurred adding message: %s.\n",
error.get_description().c_str()); error.get_msg().c_str());
notmuch->exception_reported = TRUE; notmuch->exception_reported = TRUE;
ret = NOTMUCH_STATUS_XAPIAN_EXCEPTION; ret = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
goto DONE; goto DONE;
@ -1616,7 +1630,7 @@ notmuch_database_remove_message (notmuch_database_t *notmuch,
const char *filename) const char *filename)
{ {
Xapian::WritableDatabase *db; Xapian::WritableDatabase *db;
void *local = talloc_new (notmuch); void *local;
const char *prefix = _find_prefix ("file-direntry"); const char *prefix = _find_prefix ("file-direntry");
char *direntry, *term; char *direntry, *term;
Xapian::PostingIterator i, end; Xapian::PostingIterator i, end;
@ -1627,37 +1641,47 @@ notmuch_database_remove_message (notmuch_database_t *notmuch,
if (status) if (status)
return status; return status;
local = talloc_new (notmuch);
db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db); db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
status = _notmuch_database_filename_to_direntry (local, notmuch, try {
filename, &direntry);
if (status)
return status;
term = talloc_asprintf (notmuch, "%s%s", prefix, direntry); status = _notmuch_database_filename_to_direntry (local, notmuch,
filename, &direntry);
if (status)
return status;
find_doc_ids_for_term (notmuch, term, &i, &end); term = talloc_asprintf (notmuch, "%s%s", prefix, direntry);
for ( ; i != end; i++) { find_doc_ids_for_term (notmuch, term, &i, &end);
Xapian::TermIterator j;
document = find_document_for_doc_id (notmuch, *i); for ( ; i != end; i++) {
Xapian::TermIterator j;
document.remove_term (term); document = find_document_for_doc_id (notmuch, *i);
j = document.termlist_begin (); document.remove_term (term);
j.skip_to (prefix);
/* Was this the last file-direntry in the message? */ j = document.termlist_begin ();
if (j == document.termlist_end () || j.skip_to (prefix);
strncmp ((*j).c_str (), prefix, strlen (prefix)))
{ /* Was this the last file-direntry in the message? */
db->delete_document (document.get_docid ()); if (j == document.termlist_end () ||
status = NOTMUCH_STATUS_SUCCESS; strncmp ((*j).c_str (), prefix, strlen (prefix)))
} else { {
db->replace_document (document.get_docid (), document); db->delete_document (document.get_docid ());
status = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID; status = NOTMUCH_STATUS_SUCCESS;
} else {
db->replace_document (document.get_docid (), document);
status = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;
}
} }
} catch (const Xapian::Error &error) {
fprintf (stderr, "Error: A Xapian exception occurred removing message: %s\n",
error.get_msg().c_str());
notmuch->exception_reported = TRUE;
status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
} }
talloc_free (local); talloc_free (local);
@ -1703,7 +1727,15 @@ notmuch_tags_t *
notmuch_database_get_all_tags (notmuch_database_t *db) notmuch_database_get_all_tags (notmuch_database_t *db)
{ {
Xapian::TermIterator i, end; Xapian::TermIterator i, end;
i = db->xapian_db->allterms_begin();
end = db->xapian_db->allterms_end(); try {
return _notmuch_convert_tags(db, i, end); i = db->xapian_db->allterms_begin();
end = db->xapian_db->allterms_end();
return _notmuch_convert_tags(db, i, end);
} catch (const Xapian::Error &error) {
fprintf (stderr, "A Xapian exception occurred getting tags: %s.\n",
error.get_msg().c_str());
db->exception_reported = TRUE;
return NULL;
}
} }

View file

@ -219,6 +219,8 @@ notmuch_database_upgrade (notmuch_database_t *database,
* Here, 'path' should be a path relative to the path of 'database' * Here, 'path' should be a path relative to the path of 'database'
* (see notmuch_database_get_path), or else should be an absolute path * (see notmuch_database_get_path), or else should be an absolute path
* with initial components that match the path of 'database'. * with initial components that match the path of 'database'.
*
* Can return NULL if a Xapian exception occurs.
*/ */
notmuch_directory_t * notmuch_directory_t *
notmuch_database_get_directory (notmuch_database_t *database, notmuch_database_get_directory (notmuch_database_t *database,
@ -246,6 +248,9 @@ notmuch_database_get_directory (notmuch_database_t *database,
* *
* NOTMUCH_STATUS_SUCCESS: Message successfully added to database. * NOTMUCH_STATUS_SUCCESS: Message successfully added to database.
* *
* NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred,
* message not added.
*
* NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID: Message has the same message * NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID: Message has the same message
* ID as another message already in the database. The new * ID as another message already in the database. The new
* filename was successfully added to the message in the database * filename was successfully added to the message in the database
@ -280,6 +285,9 @@ notmuch_database_add_message (notmuch_database_t *database,
* NOTMUCH_STATUS_SUCCESS: The last filename was removed and the * NOTMUCH_STATUS_SUCCESS: The last filename was removed and the
* message was removed from the database. * message was removed from the database.
* *
* NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred,
* message not removed.
*
* NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID: This filename was removed but * NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID: This filename was removed but
* the message persists in the database with at least one other * the message persists in the database with at least one other
* filename. * filename.
@ -297,8 +305,11 @@ notmuch_database_remove_message (notmuch_database_t *database,
* a new notmuch_message_t object is returned. The caller should call * a new notmuch_message_t object is returned. The caller should call
* notmuch_message_destroy when done with the message. * notmuch_message_destroy when done with the message.
* *
* If no message is found with the given message_id or if an * This function returns NULL in the following situations:
* out-of-memory situation occurs, this function returns NULL. *
* * No message is found with the given message_id
* * An out-of-memory situation occurs
* * A Xapian exception occurs
*/ */
notmuch_message_t * notmuch_message_t *
notmuch_database_find_message (notmuch_database_t *database, notmuch_database_find_message (notmuch_database_t *database,