* mu-(query|store)-xapian.cc, mu-util.h: define/use macro for catch blocks to

declutter my code
This commit is contained in:
Dirk-Jan C. Binnema 2010-01-05 08:32:23 +02:00
parent e28b9dcdf3
commit 4bc30783b1
3 changed files with 66 additions and 105 deletions

View File

@ -28,6 +28,9 @@
#include "mu-msg-xapian.h" #include "mu-msg-xapian.h"
#include "mu-msg-xapian-priv.hh" #include "mu-msg-xapian-priv.hh"
#include "mu-util.h"
static void add_prefix (const MuMsgField* field, Xapian::QueryParser* qparser); static void add_prefix (const MuMsgField* field, Xapian::QueryParser* qparser);
struct _MuQueryXapian { struct _MuQueryXapian {
@ -48,22 +51,20 @@ _init_mu_query_xapian (MuQueryXapian *mqx, const char* dbpath)
mqx->_qparser->set_database(*mqx->_db); mqx->_qparser->set_database(*mqx->_db);
mqx->_qparser->set_default_op(Xapian::Query::OP_OR); mqx->_qparser->set_default_op(Xapian::Query::OP_OR);
mqx->_qparser->set_stemming_strategy (Xapian::QueryParser::STEM_SOME); mqx->_qparser->set_stemming_strategy
(Xapian::QueryParser::STEM_SOME);
memset (mqx->_sorters, 0, sizeof(mqx->_sorters)); memset (mqx->_sorters, 0, sizeof(mqx->_sorters));
mu_msg_field_foreach ((MuMsgFieldForEachFunc)add_prefix, mu_msg_field_foreach ((MuMsgFieldForEachFunc)add_prefix,
(gpointer)mqx->_qparser); (gpointer)mqx->_qparser);
return TRUE;
} catch (...) {
g_warning ("%s: caught exception", __FUNCTION__); } MU_UTIL_XAPIAN_CATCH_BLOCK;
delete mqx->_db;
delete mqx->_qparser;
delete mqx->_db; return FALSE;
delete mqx->_qparser;
return FALSE;
}
return TRUE;
} }
@ -77,10 +78,7 @@ _uninit_mu_query_xapian (MuQueryXapian *mqx)
for (int i = 0; i != MU_MSG_FIELD_TYPE_NUM; ++i) for (int i = 0; i != MU_MSG_FIELD_TYPE_NUM; ++i)
delete mqx->_sorters[i]; delete mqx->_sorters[i];
} catch (...) { } MU_UTIL_XAPIAN_CATCH_BLOCK;
g_warning ("%s: caught exception", __FUNCTION__);
}
} }
static Xapian::Query static Xapian::Query
@ -97,14 +95,8 @@ _get_query (MuQueryXapian * mqx, const char* searchexpr, int *err = 0) {
Xapian::QueryParser::FLAG_PURE_NOT | Xapian::QueryParser::FLAG_PURE_NOT |
Xapian::QueryParser::FLAG_PARTIAL); Xapian::QueryParser::FLAG_PARTIAL);
} catch (const Xapian::Error& ex) { } MU_UTIL_XAPIAN_CATCH_BLOCK;
g_warning ("error in query: %s (\"%s\")",
ex.get_msg().c_str(), searchexpr);
} catch (...) {
g_warning ("%s: caught exception", __FUNCTION__);
}
if (err) if (err)
*err = 1; *err = 1;
@ -137,17 +129,20 @@ mu_query_xapian_new (const char* path)
g_return_val_if_fail (path, NULL); g_return_val_if_fail (path, NULL);
xpath = g_strdup_printf ("%s%c%s", path, G_DIR_SEPARATOR, "xapian"); xpath = g_strdup_printf ("%s%c%s", path, G_DIR_SEPARATOR, "xapian");
if (!g_file_test (xpath, G_FILE_TEST_IS_DIR) || if (!access(xpath, R_OK) != 0) {
g_access(xpath, R_OK) != 0) { g_warning ("'%s' is not a readable xapian dir",xpath);
g_warning ("'%s' is not a readable xapian dir", xpath);
g_free (xpath); g_free (xpath);
return NULL; return NULL;
} }
mqx = g_new (MuQueryXapian, 1); mqx = g_new (MuQueryXapian, 1);
_init_mu_query_xapian (mqx, xpath); try {
_init_mu_query_xapian (mqx, xpath);
} catch (...) {
g_free (mqx);
mqx = NULL;
}
g_free (xpath); g_free (xpath);
return mqx; return mqx;
} }
@ -186,13 +181,7 @@ mu_query_xapian_run (MuQueryXapian *self, const char* searchexpr,
return mu_msg_xapian_new (enq, 10000); return mu_msg_xapian_new (enq, 10000);
} catch (const Xapian::Error &err) { } MU_UTIL_XAPIAN_CATCH_BLOCK;
g_warning ("%s: caught xapian exception '%s' for expr '%s' (%s)",
__FUNCTION__, err.get_msg().c_str(),
searchexpr, err.get_error_string());
} catch (...) {
g_warning ("%s: caught exception", __FUNCTION__);
}
return NULL; return NULL;
} }
@ -207,15 +196,7 @@ mu_query_xapian_as_string (MuQueryXapian *self, const char* searchexpr)
Xapian::Query q(_get_query(self, searchexpr)); Xapian::Query q(_get_query(self, searchexpr));
return g_strdup(q.get_description().c_str()); return g_strdup(q.get_description().c_str());
} catch (const Xapian::Error &err) { } MU_UTIL_XAPIAN_CATCH_BLOCK_RETURN(NULL);
g_warning ("%s: caught xapian exception '%s' (%s)",
__FUNCTION__, err.get_msg().c_str(),
err.get_error_string());
} catch (...) {
g_warning ("%s: caught exception", __FUNCTION__);
}
return NULL;
} }

View File

@ -28,6 +28,7 @@
#include "mu-msg.h" #include "mu-msg.h"
#include "mu-msg-gmime.h" #include "mu-msg-gmime.h"
#include "mu-store-xapian.h" #include "mu-store-xapian.h"
#include "mu-util.h"
/* number of new messages after which we commit to the database */ /* number of new messages after which we commit to the database */
#define MU_STORE_XAPIAN_TRANSACTION_SIZE 2000 #define MU_STORE_XAPIAN_TRANSACTION_SIZE 2000
@ -61,21 +62,12 @@ mu_store_xapian_new (const char* path)
g_message ("%s: opened %s", __FUNCTION__, path); g_message ("%s: opened %s", __FUNCTION__, path);
return store; return store;
} catch (const Xapian::Error &err) { } MU_UTIL_XAPIAN_CATCH_BLOCK;
delete store->_db; delete store->_db;
g_free (store); g_free (store);
g_warning ("%s: caught xapian exception '%s' (%s)",
__FUNCTION__, err.get_msg().c_str(),
err.get_error_string());
return NULL;
} catch (...) { return NULL;
delete store->_db;
g_free (store);
g_warning ("%s: caught exception", __FUNCTION__);
return NULL;
}
} }
@ -98,15 +90,7 @@ mu_store_xapian_destroy (MuStoreXapian *store)
delete store->_db; delete store->_db;
g_free (store); g_free (store);
} catch (const Xapian::Error &err) { } MU_UTIL_XAPIAN_CATCH_BLOCK;
g_free (store);
g_warning ("%s: caught xapian exception '%s' (%s)",
__FUNCTION__, err.get_msg().c_str(),
err.get_error_string());
} catch (...) {
g_free (store);
g_warning ("%s: caught exception", __FUNCTION__);
}
} }
@ -253,13 +237,7 @@ mu_store_xapian_store (MuStoreXapian *store, MuMsgGMime *msg)
return MU_OK; return MU_OK;
} catch (const Xapian::Error &err) { } MU_UTIL_XAPIAN_CATCH_BLOCK;
g_warning ("%s: caught xapian exception '%s' (%s)",
__FUNCTION__, err.get_msg().c_str(),
err.get_error_string());
} catch (...) {
g_warning ("%s: caught exception", __FUNCTION__);
}
if (store->_in_transaction) { if (store->_in_transaction) {
store->_in_transaction = false; store->_in_transaction = false;
@ -279,16 +257,7 @@ mu_store_xapian_remove (MuStoreXapian *store, const char* msgpath)
try { try {
return MU_OK; /* FIXME: TODO: */ return MU_OK; /* FIXME: TODO: */
} catch (const Xapian::Error &err) { } MU_UTIL_XAPIAN_CATCH_BLOCK_RETURN (MU_ERROR);
g_warning ("%s: caught xapian exception '%s' (%s)",
__FUNCTION__, err.get_msg().c_str(),
err.get_error_string());
return MU_ERROR;
} catch (...) {
g_warning ("%s: caught exception", __FUNCTION__);
return MU_ERROR;
}
} }
@ -305,16 +274,7 @@ mu_store_xapian_get_timestamp (MuStoreXapian *store, const char* msgpath)
return (time_t) g_ascii_strtoull (stamp.c_str(), NULL, 10); return (time_t) g_ascii_strtoull (stamp.c_str(), NULL, 10);
} catch (const Xapian::Error &err) { } MU_UTIL_XAPIAN_CATCH_BLOCK_RETURN (0);
g_warning ("%s: caught xapian exception '%s' (%s)",
__FUNCTION__, err.get_msg().c_str(),
err.get_error_string());
return MU_ERROR;
} catch (...) {
g_warning ("%s: caught exception", __FUNCTION__);
return MU_ERROR;
}
return 0; return 0;
} }
@ -332,13 +292,7 @@ mu_store_xapian_set_timestamp (MuStoreXapian *store, const char* msgpath,
sprintf (buf, "%" G_GUINT64_FORMAT, (guint64)stamp); sprintf (buf, "%" G_GUINT64_FORMAT, (guint64)stamp);
store->_db->set_metadata (msgpath, buf); store->_db->set_metadata (msgpath, buf);
} catch (const Xapian::Error &err) { } MU_UTIL_XAPIAN_CATCH_BLOCK;
g_warning ("%s: caught xapian exception '%s' (%s)",
__FUNCTION__, err.get_msg().c_str(),
err.get_error_string());
} catch (...) {
g_warning ("%s: caught exception", __FUNCTION__);
}
} }
@ -371,13 +325,8 @@ mu_store_xapian_foreach (MuStoreXapian *self,
return res; return res;
} }
} catch (const Xapian::Error &err) { } MU_UTIL_XAPIAN_CATCH_BLOCK_RETURN (MU_ERROR);
g_warning ("%s: caught xapian exception '%s' (%s)",
__FUNCTION__, err.get_msg().c_str(),
err.get_error_string());
} catch (...) {
g_warning ("%s: caught exception", __FUNCTION__);
}
return MU_OK; return MU_OK;
} }

View File

@ -57,6 +57,37 @@ char* mu_util_guess_maildir (void);
*/ */
gboolean mu_util_create_dir_maybe (const gchar *path); gboolean mu_util_create_dir_maybe (const gchar *path);
/**
*
* don't repeat this catch blocks everywhere...
*
*/
#define MU_UTIL_XAPIAN_CATCH_BLOCK \
catch (const Xapian::Error &err) { \
g_warning ("%s: caught xapian exception '%s' (%s)", \
__FUNCTION__, err.get_msg().c_str(), \
err.get_error_string()); \
} catch (...) { \
g_warning ("%s: caught exception", __FUNCTION__); \
}
#define MU_UTIL_XAPIAN_CATCH_BLOCK_RETURN(R) \
catch (const Xapian::Error &err) { \
g_warning ("%s: caught xapian exception '%s' (%s)", \
__FUNCTION__, err.get_msg().c_str(), \
err.get_error_string()); \
return (R); \
} catch (...) { \
g_warning ("%s: caught exception", __FUNCTION__); \
return (R); \
}
G_END_DECLS G_END_DECLS
#endif /*__MU_UTIL_H__*/ #endif /*__MU_UTIL_H__*/