* add some GErrors

This commit is contained in:
Dirk-Jan C. Binnema
2010-11-25 21:49:25 +02:00
parent 7362d65216
commit e22149fa46
10 changed files with 85 additions and 46 deletions

View File

@ -134,10 +134,15 @@ save_parts (const char *path, MuConfigOptions *opts)
{
MuMsg* msg;
gboolean rv;
GError *err;
msg = mu_msg_new (path, NULL);
if (!msg)
err = NULL;
msg = mu_msg_new (path, NULL, &err);
if (!msg) {
g_warning ("Error: %s", err->message);
g_error_free (err);
return FALSE;
}
/* note, mu_cmd_extract already checks whether what's in opts
* is somewhat, so no need for extensive checking here */
@ -176,10 +181,15 @@ static gboolean
show_parts (const char* path, MuConfigOptions *opts)
{
MuMsg* msg;
GError *err;
msg = mu_msg_new (path, NULL);
if (!msg)
err = NULL;
msg = mu_msg_new (path, NULL, &err);
if (!msg) {
g_warning ("Error: %s", err->message);
g_error_free (err);
return FALSE;
}
g_print ("MIME-parts in this message:\n");
mu_msg_msg_part_foreach (msg, each_part_show, NULL);

View File

@ -39,10 +39,15 @@ view_file (const gchar *path, const gchar *fields, size_t summary_len)
MuMsg* msg;
const char *field;
time_t date;
GError *err;
msg = mu_msg_new (path, NULL);
if (!msg)
err = NULL;
msg = mu_msg_new (path, NULL, &err);
if (!msg) {
g_warning ("Error: %s", err->message);
g_error_free (err);
return FALSE;
}
if ((field = mu_msg_get_from (msg)))
g_print ("From: %s\n", field);

View File

@ -29,8 +29,14 @@ enum _MuError {
/* database version is not uptodate (ie. not compatible with
* the version that mu expects) */
MU_ERROR_XAPIAN_NOT_UPTODATE,
/* missing data for a document */
MU_ERROR_XAPIAN_MISSING_DATA,
/* (parsnng) error in the query */
MU_ERROR_QUERY,
/* file loading related error */
MU_ERROR_FILE,
/* gmime parsing related error */
MU_ERROR_GMIME,
/* some other, internal error */
MU_ERROR_INTERNAL
};

View File

@ -100,6 +100,7 @@ insert_or_update_maybe (const char* fullpath, const char* mdir,
MuIndexCallbackData *data, gboolean *updated)
{
MuMsg *msg;
GError *err;
*updated = FALSE;
@ -126,8 +127,8 @@ insert_or_update_maybe (const char* fullpath, const char* mdir,
} while (0);
msg = mu_msg_new (fullpath, mdir);
err = NULL;
msg = mu_msg_new (fullpath, mdir, &err);
if (!msg) {
g_warning ("%s: failed to create mu_msg for %s",
__FUNCTION__, fullpath);

View File

@ -82,7 +82,7 @@ mu_msg_iter_destroy (MuMsgIter *iter)
MuMsg*
mu_msg_iter_get_msg (MuMsgIter *iter)
mu_msg_iter_get_msg (MuMsgIter *iter, GError **err)
{
const char *path;
MuMsg *msg;
@ -92,15 +92,14 @@ mu_msg_iter_get_msg (MuMsgIter *iter)
path = mu_msg_iter_get_path (iter);
if (!path) {
g_warning ("%s: no path for message", __FUNCTION__);
g_set_error (err, 0, MU_ERROR_XAPIAN_MISSING_DATA,
"no path for message");
return NULL;
}
msg = mu_msg_new (path, NULL);
if (!msg) {
g_warning ("%s: failed to create msg object", __FUNCTION__);
msg = mu_msg_new (path, NULL, err);
if (!msg)
return NULL;
}
return msg;
}

View File

@ -28,7 +28,7 @@ struct _MuMsgIter;
typedef struct _MuMsgIter MuMsgIter;
/**
* get the next next message (which you got from
* get the next message (which you got from
* e.g. mu_query_run)
*
* @param iter a valid MuMsgIter iterator
@ -67,11 +67,13 @@ void mu_msg_iter_destroy (MuMsgIter *iter);
* provided from the iter).
*
* @param iter a valid MuMsgIter instance
* @param err which receives error info or NULL. err is only filled
* when the function returns NULL
*
* @return a MuMsgGMime instance, or NULL in case of error. Use
* mu_msg_gmime_destroy when the instance is no longer needed
*/
MuMsg* mu_msg_iter_get_msg (MuMsgIter *iter);
MuMsg* mu_msg_iter_get_msg (MuMsgIter *iter, GError **err);
/**
* get the document id for the current message

View File

@ -78,25 +78,28 @@ mu_msg_destroy (MuMsg *msg)
static gboolean
init_file_metadata (MuMsg* msg, const char* path, const gchar* mdir)
init_file_metadata (MuMsg* msg, const char* path, const gchar* mdir,
GError **err)
{
struct stat statbuf;
if (access (path, R_OK) != 0) {
g_warning ("%s: cannot read file %s: %s",
__FUNCTION__, path, strerror(errno));
g_set_error (err, 0, MU_ERROR_FILE,
"cannot read file %s: %s",
path, strerror(errno));
return FALSE;
}
if (stat (path, &statbuf) < 0) {
g_warning ("%s: cannot stat %s: %s",
__FUNCTION__, path, strerror(errno));
g_set_error (err, 0, MU_ERROR_FILE,
"cannot stat %s: %s",
path, strerror(errno));
return FALSE;
}
if (!S_ISREG(statbuf.st_mode)) {
g_warning ("%s: not a regular file: %s",
__FUNCTION__, path);
g_set_error (err, 0, MU_ERROR_FILE,
"not a regular file: %s", path);
return FALSE;
}
@ -114,7 +117,7 @@ init_file_metadata (MuMsg* msg, const char* path, const gchar* mdir)
static gboolean
init_mime_msg (MuMsg *msg)
init_mime_msg (MuMsg *msg, GError **err)
{
FILE *file;
GMimeStream *stream;
@ -122,15 +125,17 @@ init_mime_msg (MuMsg *msg)
file = fopen (mu_msg_get_path(msg), "r");
if (!file) {
g_warning ("%s:cannot open %s: %s",
__FUNCTION__, mu_msg_get_path(msg),
g_set_error (err, 0, MU_ERROR_FILE,
"cannot open %s: %s", mu_msg_get_path(msg),
strerror (errno));
return FALSE;
}
stream = g_mime_stream_file_new (file);
if (!stream) {
g_warning ("%s: cannot create mime stream", __FUNCTION__);
g_set_error (err, 0, MU_ERROR_GMIME,
"cannot create mime stream for %s",
mu_msg_get_path(msg));
fclose (file);
return FALSE;
}
@ -138,14 +143,18 @@ init_mime_msg (MuMsg *msg)
parser = g_mime_parser_new_with_stream (stream);
g_object_unref (stream);
if (!parser) {
g_warning ("%s: cannot create mime parser", __FUNCTION__);
g_set_error (err, 0, MU_ERROR_GMIME,
"cannot create mime parser for %s",
mu_msg_get_path(msg));
return FALSE;
}
msg->_mime_msg = g_mime_parser_construct_message (parser);
g_object_unref (parser);
if (!msg->_mime_msg) {
g_warning ("%s: cannot create mime message", __FUNCTION__);
g_set_error (err, 0, MU_ERROR_GMIME,
"cannot construct mime message for %s",
mu_msg_get_path(msg));
return FALSE;
}
@ -154,7 +163,7 @@ init_mime_msg (MuMsg *msg)
MuMsg*
mu_msg_new (const char* filepath, const gchar* mdir)
mu_msg_new (const char* filepath, const gchar* mdir, GError **err)
{
MuMsg *msg;
@ -165,12 +174,12 @@ mu_msg_new (const char* filepath, const gchar* mdir)
msg = g_slice_new0 (MuMsg);
msg->_prio = MU_MSG_PRIO_NONE;
if (!init_file_metadata(msg, filepath, mdir)) {
if (!init_file_metadata(msg, filepath, mdir, err)) {
mu_msg_destroy (msg);
return NULL;
}
if (!init_mime_msg(msg)) {
if (!init_mime_msg(msg, err)) {
mu_msg_destroy (msg);
return NULL;
}

View File

@ -24,6 +24,7 @@
#include "mu-msg-fields.h"
#include "mu-msg-status.h"
#include "mu-msg-prio.h"
#include "mu-error.h"
G_BEGIN_DECLS
@ -35,16 +36,18 @@ typedef struct _MuMsg MuMsg;
* read access to its properties; call mu_msg_destroy when done with it.
*
* @param path full path to an email message file
*
* @param mdir the maildir for this message; ie, if the path is
* ~/Maildir/foo/bar/cur/msg, the maildir would be foo/bar; you can
* pass NULL for this parameter, in which case some maildir-specific
* information is not available.
* @param err receive error information (MU_ERROR_FILE or MU_ERROR_GMIME), or NULL. There
* will only be err info if the function returns NULL
*
* @return a new MuMsg instance or NULL in case of error
*/
MuMsg* mu_msg_new (const char* filepath,
const char *maildir);
const char *maildir,
GError **err);
/**

View File

@ -75,12 +75,15 @@ display_field (MuMsgIter *iter, MuMsgFieldId mfid)
static void
print_summary (MuMsgIter *iter, size_t summary_len)
{
GError *err;
const char *summ;
MuMsg *msg;
msg = mu_msg_iter_get_msg (iter);
err = NULL;
msg = mu_msg_iter_get_msg (iter, &err);
if (!msg) {
g_warning ("%s: failed to create msg object", __FUNCTION__);
g_warning ("error get message: %s", err->message);
g_error_free (err);
return;
}

View File

@ -117,7 +117,8 @@ private:
date = cleanup;
}
void complete_date (std::string& date, size_t len, bool is_begin) const {
void complete_date (std::string& date, size_t len,
bool is_begin) const {
const std::string bsuffix ("00000101000000");
const std::string esuffix ("99991231235959");