diff --git a/src/Makefile.am b/src/Makefile.am index 8fed6eae..0607ed91 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -57,6 +57,7 @@ libmu_la_SOURCES= \ mu-cmd.h \ mu-config.c \ mu-config.h \ + mu-error.h \ mu-index.c \ mu-index.h \ mu-log.c \ diff --git a/src/mu-cmd-index.c b/src/mu-cmd-index.c index d106cacd..26e425e0 100644 --- a/src/mu-cmd-index.c +++ b/src/mu-cmd-index.c @@ -260,16 +260,21 @@ cmd_index_or_cleanup (MuConfigOptions *opts) MuIndex *midx; MuIndexStats stats; gboolean show_progress; + GError *err; g_return_val_if_fail (opts, FALSE); g_return_val_if_fail (mu_cmd_equals (opts, "index") || mu_cmd_equals (opts, "cleanup"), FALSE); - if (!check_index_params (opts) || !database_version_check_and_update(opts)) + if (!check_index_params (opts) || + !database_version_check_and_update(opts)) return FALSE; - - if (!(midx = mu_index_new (mu_runtime_xapian_dir()))) { - g_warning ("Indexing/Cleanup failed"); + + err = NULL; + if (!(midx = mu_index_new (mu_runtime_xapian_dir(), &err))) { + g_warning ("Indexing/Cleanup failed: %s", + err->message); + g_error_free (err); return FALSE; } diff --git a/src/mu-error.h b/src/mu-error.h new file mode 100644 index 00000000..3d3f278a --- /dev/null +++ b/src/mu-error.h @@ -0,0 +1,39 @@ +/* +** Copyright (C) 2010 Dirk-Jan C. Binnema +** +** This program is free software; you can redistribute it and/or modify it +** under the terms of the GNU General Public License as published by the +** Free Software Foundation; either version 3, or (at your option) any +** later version. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU General Public License for more details. +** +** You should have received a copy of the GNU General Public License +** along with this program; if not, write to the Free Software Foundation, +** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +** +*/ + + +#ifndef __MU_ERROR_H__ +#define __MU_ERROR_H__ + +enum _MuError { + /* general xapian related error */ + MU_ERROR_XAPIAN, + /* xapian dir is not accessible */ + MU_ERROR_XAPIAN_DIR, + /* database version is not uptodate (ie. not compatible with + * the version that mu expects) */ + MU_ERROR_XAPIAN_NOT_UPTODATE, + /* (parsnng) error in the query */ + MU_ERROR_QUERY, + /* some other, internal error */ + MU_ERROR_INTERNAL +}; +typedef enum _MuError MuError; + +#endif /*__MU_ERROR_H__*/ diff --git a/src/mu-index.c b/src/mu-index.c index a95eaf97..4ac0f99f 100644 --- a/src/mu-index.c +++ b/src/mu-index.c @@ -41,14 +41,14 @@ struct _MuIndex { }; MuIndex* -mu_index_new (const char *xpath) +mu_index_new (const char *xpath, GError **err) { MuIndex *index; g_return_val_if_fail (xpath, NULL); index = g_new0 (MuIndex, 1); - index->_xapian = mu_store_new (xpath); + index->_xapian = mu_store_new (xpath, err); if (!index->_xapian) { g_warning ("%s: failed to open xapian store (%s)", @@ -57,9 +57,10 @@ mu_index_new (const char *xpath) return NULL; } - /* see we need to reindex the database; note, there is a small race-condition - * here, between mu_index_new and mu_index_run. Maybe do the check in - * mu_index_run instead? */ + /* see we need to reindex the database; note, there is a small + * race-condition here, between mu_index_new and + * mu_index_run. Maybe do the check in mu_index_run + * instead? */ if (mu_util_db_is_empty (xpath)) index->_needs_reindex = FALSE; else diff --git a/src/mu-index.h b/src/mu-index.h index 64e7eb04..6ec1f3f0 100644 --- a/src/mu-index.h +++ b/src/mu-index.h @@ -45,10 +45,13 @@ typedef struct _MuIndexStats MuIndexStats; * * @param xpath path to the 'homedir'; the xapian directory will be * this homedir/xapian + * + * @param err to receive error or NULL; there are only errors when this + * function returns NULL. Possible errors: see mu-error.h * * @return a new MuIndex instance, or NULL in case of error */ -MuIndex* mu_index_new (const char* muhome); +MuIndex* mu_index_new (const char* muhome, GError **err); /** diff --git a/src/mu-query.h b/src/mu-query.h index 925c618a..1d965645 100644 --- a/src/mu-query.h +++ b/src/mu-query.h @@ -21,7 +21,8 @@ #define __MU_QUERY_H__ #include -#include "mu-msg-iter.h" +#include +#include G_BEGIN_DECLS @@ -34,6 +35,8 @@ typedef struct _MuQuery MuQuery; * @param path path to the xapian db to search * @param err receives error information (if there is any); if * function returns non-NULL, err will _not_be set. err can be NULL + * possble errors (err->code) are MU_ERROR_XAPIAN_DIR and + * MU_ERROR_XAPIAN_NOT_UPTODATE * * @return a new MuQuery instance, or NULL in case of error. * when the instance is no longer needed, use mu_query_destroy @@ -74,6 +77,7 @@ char* mu_query_version (MuQuery *store) G_GNUC_WARN_UNUSED_RESULT; * of documents in the database) * @param err receives error information (if there is any); if * function returns non-NULL, err will _not_be set. err can be NULL + * possible error (err->code) is MU_ERROR_QUERY, * * @return a MuMsgIter instance you can iterate over, or NULL in * case of error diff --git a/src/mu-result.h b/src/mu-result.h index 38411854..ce91762d 100644 --- a/src/mu-result.h +++ b/src/mu-result.h @@ -22,17 +22,8 @@ enum _MuResult { MU_OK, /* all went ok */ - MU_STOP, /* user wants to stop */ - - MU_ERROR_XAPIAN_DIR, - MU_ERROR_XAPIAN_NOT_UPTODATE, - MU_ERROR_QUERY, - MU_ERROR_INTERNAL, - + MU_STOP, /* user wants to stop */ MU_ERROR /* some other error occured */ - - - }; typedef enum _MuResult MuResult; diff --git a/src/mu-store.cc b/src/mu-store.cc index e48294f2..1d56975a 100644 --- a/src/mu-store.cc +++ b/src/mu-store.cc @@ -115,7 +115,7 @@ check_version (MuStore *store) } MuStore* -mu_store_new (const char* xpath) +mu_store_new (const char* xpath, GError **err) { MuStore *store (0); @@ -141,7 +141,7 @@ mu_store_new (const char* xpath) return store; - } MU_XAPIAN_CATCH_BLOCK; + } MU_XAPIAN_CATCH_BLOCK_G_ERROR(err,MU_ERROR_XAPIAN); try { delete store->_db; } MU_XAPIAN_CATCH_BLOCK; diff --git a/src/mu-store.h b/src/mu-store.h index bfea5534..4674531f 100644 --- a/src/mu-store.h +++ b/src/mu-store.h @@ -25,6 +25,7 @@ #include "mu-result.h" #include "mu-msg.h" +#include "mu-error.h" G_BEGIN_DECLS @@ -35,10 +36,12 @@ typedef struct _MuStore MuStore; * create a new Xapian store, a place to store documents * * @param path the path to the database + * @param err to receive error info or NULL. err->code can be found in + * mu-error.h * * @return a new MuStore object, or NULL in case of error */ -MuStore* mu_store_new (const char* path); +MuStore* mu_store_new (const char* path, GError **err); /** * destroy the MuStore object and free resources @@ -78,7 +81,7 @@ const char* mu_store_version (MuStore *store); * @return TRUE if setting the version succeeded, FALSE otherwise */ gboolean mu_store_set_version (MuStore *store, - const char* version); + const char* version); /** diff --git a/src/mu-util.h b/src/mu-util.h index 0717016a..e7fa216d 100644 --- a/src/mu-util.h +++ b/src/mu-util.h @@ -120,24 +120,48 @@ gchar* mu_util_str_from_strv (const gchar **params) G_GNUC_WARN_UNUSED_RESULT; */ #define MU_XAPIAN_CATCH_BLOCK \ - catch (const Xapian::Error &err) { \ + catch (const Xapian::Error &xerr) { \ g_critical ("%s: caught xapian exception '%s'", \ - __FUNCTION__, err.get_msg().c_str()); \ + __FUNCTION__, xerr.get_msg().c_str()); \ } catch (...) { \ g_critical ("%s: caught exception", __FUNCTION__); \ } +#define MU_XAPIAN_CATCH_BLOCK_G_ERROR(GE,E) \ + catch (const Xapian::Error &xerr) { \ + g_set_error ((GE),0,(E), \ + "%s: caught xapian exception '%s'", \ + __FUNCTION__, xerr.get_msg().c_str()); \ + } catch (...) { \ + g_set_error ((GE),0,(MU_ERROR_INTERNAL), \ + "%s: caught exception", __FUNCTION__); \ + } + + #define MU_XAPIAN_CATCH_BLOCK_RETURN(R) \ - catch (const Xapian::Error &err) { \ + catch (const Xapian::Error &xerr) { \ g_critical ("%s: caught xapian exception '%s'", \ - __FUNCTION__, err.get_msg().c_str()); \ + __FUNCTION__, xerr.get_msg().c_str()); \ return (R); \ } catch (...) { \ g_critical ("%s: caught exception", __FUNCTION__); \ return (R); \ } +#define MU_XAPIAN_CATCH_BLOCK_G_ERROR_RETURN(GE,E,R) \ + catch (const Xapian::Error &xerr) { \ + g_set_error ((GE),0,(E), \ + "%s: caught xapian exception '%s'", \ + __FUNCTION__, xerr.get_msg().c_str()); \ + return (R); \ + } catch (...) { \ + g_set_error ((GE),0,(MU_ERROR_INTERNAL), \ + "%s: caught exception", __FUNCTION__); \ + return (R); \ + } + + /* the name of the (leaf) dir which has the xapian database */ #define MU_XAPIAN_DIR_NAME "xapian" #define MU_XAPIAN_VERSION_KEY "db_version" diff --git a/src/tests/Makefile.am b/src/tests/Makefile.am index c55bd785..0b6cf550 100644 --- a/src/tests/Makefile.am +++ b/src/tests/Makefile.am @@ -19,7 +19,7 @@ include $(top_srcdir)/gtest.mk INCLUDES=$(XAPIAN_CXXFLAGS) \ $(GMIME_CFLAGS) \ $(GLIB_CFLAGS) \ - -I ${top_srcdir} \ + -I ${top_srcdir} -I ${top_srcdir}/src \ -DMU_TESTMAILDIR=\"${abs_srcdir}/testdir/\" \ -DMU_TESTMAILDIR2=\"${abs_srcdir}/testdir2/\" \ -DMU_PROGRAM=\"${abs_top_builddir}/src/mu\" \ diff --git a/src/tests/test-mu-cmd.c b/src/tests/test-mu-cmd.c index e7e6af18..f7973b51 100644 --- a/src/tests/test-mu-cmd.c +++ b/src/tests/test-mu-cmd.c @@ -113,7 +113,7 @@ test_mu_index (void) xpath = g_strdup_printf ("%s%c%s", muhome, G_DIR_SEPARATOR, "xapian"); - store = mu_store_new (xpath); + store = mu_store_new (xpath, NULL); g_assert (store); g_assert_cmpuint (mu_store_count (store), ==, 4);