mirror of
https://git.notmuchmail.org/git/notmuch
synced 2024-11-22 10:58:10 +01:00
lib/n_d_get_version: catch exceptions and clarify the API
notmuch_database_get_version previously returned 0 on some errors, but did not document this. Luckily 0 is not a valid database version.
This commit is contained in:
parent
095d3d7134
commit
ab45654192
3 changed files with 24 additions and 3 deletions
|
@ -58,6 +58,17 @@ typedef struct {
|
||||||
#define DB_ACTION Xapian::DB_CREATE_OR_OPEN
|
#define DB_ACTION Xapian::DB_CREATE_OR_OPEN
|
||||||
#endif
|
#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 at %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):
|
/* Here's the current schema for our database (for NOTMUCH_DATABASE_VERSION):
|
||||||
*
|
*
|
||||||
* We currently have three different types of documents (mail, ghost,
|
* 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;
|
const char *str;
|
||||||
char *end;
|
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 ())
|
if (version_string.empty ())
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
|
|
@ -431,6 +431,8 @@ notmuch_database_get_path (notmuch_database_t *database);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the database format version of the given database.
|
* Return the database format version of the given database.
|
||||||
|
*
|
||||||
|
* @retval 0 on error
|
||||||
*/
|
*/
|
||||||
unsigned int
|
unsigned int
|
||||||
notmuch_database_get_version (notmuch_database_t *database);
|
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
|
* fail with NOTMUCH_STATUS_UPGRADE_REQUIRED. This always returns
|
||||||
* FALSE for a read-only database because there's no way to upgrade a
|
* FALSE for a read-only database because there's no way to upgrade a
|
||||||
* read-only database.
|
* read-only database.
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
notmuch_bool_t
|
notmuch_bool_t
|
||||||
notmuch_database_needs_upgrade (notmuch_database_t *database);
|
notmuch_database_needs_upgrade (notmuch_database_t *database);
|
||||||
|
|
|
@ -15,7 +15,7 @@ cat <<EOF > c_head
|
||||||
int main (int argc, char** argv)
|
int main (int argc, char** argv)
|
||||||
{
|
{
|
||||||
notmuch_database_t *db;
|
notmuch_database_t *db;
|
||||||
notmuch_status_t stat;
|
notmuch_status_t stat = NOTMUCH_STATUS_SUCCESS;
|
||||||
char *msg = NULL;
|
char *msg = NULL;
|
||||||
|
|
||||||
stat = notmuch_database_open_verbose (argv[1], NOTMUCH_DATABASE_MODE_READ_WRITE, &db, &msg);
|
stat = notmuch_database_open_verbose (argv[1], NOTMUCH_DATABASE_MODE_READ_WRITE, &db, &msg);
|
||||||
|
@ -68,19 +68,20 @@ EOF
|
||||||
test_expect_equal_file EXPECTED OUTPUT
|
test_expect_equal_file EXPECTED OUTPUT
|
||||||
|
|
||||||
test_begin_subtest "get version with closed db"
|
test_begin_subtest "get version with closed db"
|
||||||
test_subtest_known_broken
|
|
||||||
cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
|
cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
|
||||||
{
|
{
|
||||||
unsigned int version;
|
unsigned int version;
|
||||||
EXPECT0(notmuch_database_close (db));
|
EXPECT0(notmuch_database_close (db));
|
||||||
version = notmuch_database_get_version (db);
|
version = notmuch_database_get_version (db);
|
||||||
printf ("%u\n", version);
|
printf ("%u\n", version);
|
||||||
|
stat = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
|
||||||
}
|
}
|
||||||
EOF
|
EOF
|
||||||
cat <<EOF > EXPECTED
|
cat <<EOF > EXPECTED
|
||||||
== stdout ==
|
== stdout ==
|
||||||
0
|
0
|
||||||
== stderr ==
|
== stderr ==
|
||||||
|
A Xapian exception occurred at lib/database.cc:XXX: Database has been closed
|
||||||
EOF
|
EOF
|
||||||
test_expect_equal_file EXPECTED OUTPUT
|
test_expect_equal_file EXPECTED OUTPUT
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue