* create mu-error.h, update some functions to use GError for errors

This commit is contained in:
Dirk-Jan C. Binnema 2010-11-24 20:04:20 +02:00
parent a5b913e66c
commit e64e24aa61
12 changed files with 102 additions and 31 deletions

View File

@ -57,6 +57,7 @@ libmu_la_SOURCES= \
mu-cmd.h \ mu-cmd.h \
mu-config.c \ mu-config.c \
mu-config.h \ mu-config.h \
mu-error.h \
mu-index.c \ mu-index.c \
mu-index.h \ mu-index.h \
mu-log.c \ mu-log.c \

View File

@ -260,16 +260,21 @@ cmd_index_or_cleanup (MuConfigOptions *opts)
MuIndex *midx; MuIndex *midx;
MuIndexStats stats; MuIndexStats stats;
gboolean show_progress; gboolean show_progress;
GError *err;
g_return_val_if_fail (opts, FALSE); g_return_val_if_fail (opts, FALSE);
g_return_val_if_fail (mu_cmd_equals (opts, "index") || g_return_val_if_fail (mu_cmd_equals (opts, "index") ||
mu_cmd_equals (opts, "cleanup"), FALSE); 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; return FALSE;
if (!(midx = mu_index_new (mu_runtime_xapian_dir()))) { err = NULL;
g_warning ("Indexing/Cleanup failed"); if (!(midx = mu_index_new (mu_runtime_xapian_dir(), &err))) {
g_warning ("Indexing/Cleanup failed: %s",
err->message);
g_error_free (err);
return FALSE; return FALSE;
} }

39
src/mu-error.h Normal file
View File

@ -0,0 +1,39 @@
/*
** Copyright (C) 2010 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** 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__*/

View File

@ -41,14 +41,14 @@ struct _MuIndex {
}; };
MuIndex* MuIndex*
mu_index_new (const char *xpath) mu_index_new (const char *xpath, GError **err)
{ {
MuIndex *index; MuIndex *index;
g_return_val_if_fail (xpath, NULL); g_return_val_if_fail (xpath, NULL);
index = g_new0 (MuIndex, 1); index = g_new0 (MuIndex, 1);
index->_xapian = mu_store_new (xpath); index->_xapian = mu_store_new (xpath, err);
if (!index->_xapian) { if (!index->_xapian) {
g_warning ("%s: failed to open xapian store (%s)", g_warning ("%s: failed to open xapian store (%s)",
@ -57,9 +57,10 @@ mu_index_new (const char *xpath)
return NULL; return NULL;
} }
/* see we need to reindex the database; note, there is a small race-condition /* see we need to reindex the database; note, there is a small
* here, between mu_index_new and mu_index_run. Maybe do the check in * race-condition here, between mu_index_new and
* mu_index_run instead? */ * mu_index_run. Maybe do the check in mu_index_run
* instead? */
if (mu_util_db_is_empty (xpath)) if (mu_util_db_is_empty (xpath))
index->_needs_reindex = FALSE; index->_needs_reindex = FALSE;
else else

View File

@ -45,10 +45,13 @@ typedef struct _MuIndexStats MuIndexStats;
* *
* @param xpath path to the 'homedir'; the xapian directory will be * @param xpath path to the 'homedir'; the xapian directory will be
* this homedir/xapian * 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 * @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);
/** /**

View File

@ -21,7 +21,8 @@
#define __MU_QUERY_H__ #define __MU_QUERY_H__
#include <glib.h> #include <glib.h>
#include "mu-msg-iter.h" #include <mu-msg-iter.h>
#include <mu-error.h>
G_BEGIN_DECLS G_BEGIN_DECLS
@ -34,6 +35,8 @@ typedef struct _MuQuery MuQuery;
* @param path path to the xapian db to search * @param path path to the xapian db to search
* @param err receives error information (if there is any); if * @param err receives error information (if there is any); if
* function returns non-NULL, err will _not_be set. err can be NULL * 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. * @return a new MuQuery instance, or NULL in case of error.
* when the instance is no longer needed, use mu_query_destroy * 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) * of documents in the database)
* @param err receives error information (if there is any); if * @param err receives error information (if there is any); if
* function returns non-NULL, err will _not_be set. err can be NULL * 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 * @return a MuMsgIter instance you can iterate over, or NULL in
* case of error * case of error

View File

@ -22,17 +22,8 @@
enum _MuResult { enum _MuResult {
MU_OK, /* all went ok */ MU_OK, /* all went ok */
MU_STOP, /* user wants to stop */ MU_STOP, /* user wants to stop */
MU_ERROR_XAPIAN_DIR,
MU_ERROR_XAPIAN_NOT_UPTODATE,
MU_ERROR_QUERY,
MU_ERROR_INTERNAL,
MU_ERROR /* some other error occured */ MU_ERROR /* some other error occured */
}; };
typedef enum _MuResult MuResult; typedef enum _MuResult MuResult;

View File

@ -115,7 +115,7 @@ check_version (MuStore *store)
} }
MuStore* MuStore*
mu_store_new (const char* xpath) mu_store_new (const char* xpath, GError **err)
{ {
MuStore *store (0); MuStore *store (0);
@ -141,7 +141,7 @@ mu_store_new (const char* xpath)
return store; return store;
} MU_XAPIAN_CATCH_BLOCK; } MU_XAPIAN_CATCH_BLOCK_G_ERROR(err,MU_ERROR_XAPIAN);
try { delete store->_db; } MU_XAPIAN_CATCH_BLOCK; try { delete store->_db; } MU_XAPIAN_CATCH_BLOCK;

View File

@ -25,6 +25,7 @@
#include "mu-result.h" #include "mu-result.h"
#include "mu-msg.h" #include "mu-msg.h"
#include "mu-error.h"
G_BEGIN_DECLS G_BEGIN_DECLS
@ -35,10 +36,12 @@ typedef struct _MuStore MuStore;
* create a new Xapian store, a place to store documents * create a new Xapian store, a place to store documents
* *
* @param path the path to the database * @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 * @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 * 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 * @return TRUE if setting the version succeeded, FALSE otherwise
*/ */
gboolean mu_store_set_version (MuStore *store, gboolean mu_store_set_version (MuStore *store,
const char* version); const char* version);
/** /**

View File

@ -120,24 +120,48 @@ gchar* mu_util_str_from_strv (const gchar **params) G_GNUC_WARN_UNUSED_RESULT;
*/ */
#define MU_XAPIAN_CATCH_BLOCK \ #define MU_XAPIAN_CATCH_BLOCK \
catch (const Xapian::Error &err) { \ catch (const Xapian::Error &xerr) { \
g_critical ("%s: caught xapian exception '%s'", \ g_critical ("%s: caught xapian exception '%s'", \
__FUNCTION__, err.get_msg().c_str()); \ __FUNCTION__, xerr.get_msg().c_str()); \
} catch (...) { \ } catch (...) { \
g_critical ("%s: caught exception", __FUNCTION__); \ 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) \ #define MU_XAPIAN_CATCH_BLOCK_RETURN(R) \
catch (const Xapian::Error &err) { \ catch (const Xapian::Error &xerr) { \
g_critical ("%s: caught xapian exception '%s'", \ g_critical ("%s: caught xapian exception '%s'", \
__FUNCTION__, err.get_msg().c_str()); \ __FUNCTION__, xerr.get_msg().c_str()); \
return (R); \ return (R); \
} catch (...) { \ } catch (...) { \
g_critical ("%s: caught exception", __FUNCTION__); \ g_critical ("%s: caught exception", __FUNCTION__); \
return (R); \ 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 */ /* the name of the (leaf) dir which has the xapian database */
#define MU_XAPIAN_DIR_NAME "xapian" #define MU_XAPIAN_DIR_NAME "xapian"
#define MU_XAPIAN_VERSION_KEY "db_version" #define MU_XAPIAN_VERSION_KEY "db_version"

View File

@ -19,7 +19,7 @@ include $(top_srcdir)/gtest.mk
INCLUDES=$(XAPIAN_CXXFLAGS) \ INCLUDES=$(XAPIAN_CXXFLAGS) \
$(GMIME_CFLAGS) \ $(GMIME_CFLAGS) \
$(GLIB_CFLAGS) \ $(GLIB_CFLAGS) \
-I ${top_srcdir} \ -I ${top_srcdir} -I ${top_srcdir}/src \
-DMU_TESTMAILDIR=\"${abs_srcdir}/testdir/\" \ -DMU_TESTMAILDIR=\"${abs_srcdir}/testdir/\" \
-DMU_TESTMAILDIR2=\"${abs_srcdir}/testdir2/\" \ -DMU_TESTMAILDIR2=\"${abs_srcdir}/testdir2/\" \
-DMU_PROGRAM=\"${abs_top_builddir}/src/mu\" \ -DMU_PROGRAM=\"${abs_top_builddir}/src/mu\" \

View File

@ -113,7 +113,7 @@ test_mu_index (void)
xpath = g_strdup_printf ("%s%c%s", muhome, G_DIR_SEPARATOR, xpath = g_strdup_printf ("%s%c%s", muhome, G_DIR_SEPARATOR,
"xapian"); "xapian");
store = mu_store_new (xpath); store = mu_store_new (xpath, NULL);
g_assert (store); g_assert (store);
g_assert_cmpuint (mu_store_count (store), ==, 4); g_assert_cmpuint (mu_store_count (store), ==, 4);