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

View File

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

View File

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

View File

@ -82,7 +82,7 @@ mu_msg_iter_destroy (MuMsgIter *iter)
MuMsg* MuMsg*
mu_msg_iter_get_msg (MuMsgIter *iter) mu_msg_iter_get_msg (MuMsgIter *iter, GError **err)
{ {
const char *path; const char *path;
MuMsg *msg; MuMsg *msg;
@ -92,15 +92,14 @@ mu_msg_iter_get_msg (MuMsgIter *iter)
path = mu_msg_iter_get_path (iter); path = mu_msg_iter_get_path (iter);
if (!path) { 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; return NULL;
} }
msg = mu_msg_new (path, NULL); msg = mu_msg_new (path, NULL, err);
if (!msg) { if (!msg)
g_warning ("%s: failed to create msg object", __FUNCTION__);
return NULL; return NULL;
}
return msg; return msg;
} }

View File

@ -28,7 +28,7 @@ struct _MuMsgIter;
typedef struct _MuMsgIter 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) * e.g. mu_query_run)
* *
* @param iter a valid MuMsgIter iterator * @param iter a valid MuMsgIter iterator
@ -67,11 +67,13 @@ void mu_msg_iter_destroy (MuMsgIter *iter);
* provided from the iter). * provided from the iter).
* *
* @param iter a valid MuMsgIter instance * @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 * @return a MuMsgGMime instance, or NULL in case of error. Use
* mu_msg_gmime_destroy when the instance is no longer needed * 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 * get the document id for the current message

View File

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

View File

@ -24,6 +24,7 @@
#include "mu-msg-fields.h" #include "mu-msg-fields.h"
#include "mu-msg-status.h" #include "mu-msg-status.h"
#include "mu-msg-prio.h" #include "mu-msg-prio.h"
#include "mu-error.h"
G_BEGIN_DECLS G_BEGIN_DECLS
@ -35,16 +36,18 @@ typedef struct _MuMsg MuMsg;
* read access to its properties; call mu_msg_destroy when done with it. * read access to its properties; call mu_msg_destroy when done with it.
* *
* @param path full path to an email message file * @param path full path to an email message file
*
* @param mdir the maildir for this message; ie, if the path is * @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 * ~/Maildir/foo/bar/cur/msg, the maildir would be foo/bar; you can
* pass NULL for this parameter, in which case some maildir-specific * pass NULL for this parameter, in which case some maildir-specific
* information is not available. * 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 * @return a new MuMsg instance or NULL in case of error
*/ */
MuMsg* mu_msg_new (const char* filepath, 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 static void
print_summary (MuMsgIter *iter, size_t summary_len) print_summary (MuMsgIter *iter, size_t summary_len)
{ {
GError *err;
const char *summ; const char *summ;
MuMsg *msg; MuMsg *msg;
msg = mu_msg_iter_get_msg (iter); err = NULL;
msg = mu_msg_iter_get_msg (iter, &err);
if (!msg) { if (!msg) {
g_warning ("%s: failed to create msg object", __FUNCTION__); g_warning ("error get message: %s", err->message);
g_error_free (err);
return; return;
} }

View File

@ -117,7 +117,8 @@ private:
date = cleanup; 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 bsuffix ("00000101000000");
const std::string esuffix ("99991231235959"); const std::string esuffix ("99991231235959");