From: Carl Worth Date: Sun, 25 Oct 2009 17:22:41 +0000 (-0700) Subject: Add an INTERNAL_ERROR macro and use it for all internal errors. X-Git-Tag: 0.1~713 X-Git-Url: https://git.notmuchmail.org/git?p=notmuch;a=commitdiff_plain;h=7b227a6bf7eb409b4353adc430b2545166e0c4cd Add an INTERNAL_ERROR macro and use it for all internal errors. We were previously just doing fprintf;exit at each point, but I wanted to add file and line-number details to all messages, so it makes sense to use a single macro for that. --- diff --git a/database.cc b/database.cc index 3f8ea903..3f020227 100644 --- a/database.cc +++ b/database.cc @@ -124,8 +124,7 @@ _find_prefix (const char *name) if (strcmp (name, BOOLEAN_PREFIX_EXTERNAL[i].name) == 0) return BOOLEAN_PREFIX_EXTERNAL[i].prefix; - fprintf (stderr, "Internal error: No prefix exists for '%s'\n", name); - exit (1); + INTERNAL_ERROR ("No prefix exists for '%s'\n", name); return ""; } diff --git a/message-file.c b/message-file.c index fd7e68b4..cb2bf665 100644 --- a/message-file.c +++ b/message-file.c @@ -125,10 +125,8 @@ notmuch_message_file_restrict_headersv (notmuch_message_file_t *message, { char *header; - if (message->parsing_started ) { - fprintf (stderr, "Error: notmuch_message_file_restrict_headers called after parsing has started\n"); - exit (1); - } + if (message->parsing_started) + INTERNAL_ERROR ("notmuch_message_file_restrict_headers called after parsing has started"); while (1) { header = va_arg (va_headers, char*); @@ -305,11 +303,9 @@ notmuch_message_file_get_header (notmuch_message_file_t *message, ! g_hash_table_lookup_extended (message->headers, header_desired, NULL, NULL)) { - fprintf (stderr, - "Internal error: Attempt to get header \"%s\" which was not\n" - "included in call to notmuch_message_file_restrict_headers\n", - header_desired); - exit (1); + INTERNAL_ERROR ("Attempt to get header \"%s\" which was not\n" + "included in call to notmuch_message_file_restrict_headers\n", + header_desired); } return NULL; diff --git a/message.cc b/message.cc index b66ca7e2..7a6c01d2 100644 --- a/message.cc +++ b/message.cc @@ -197,8 +197,7 @@ _notmuch_message_create_for_message_id (const void *talloc_owner, if (private_status >= (notmuch_private_status_t) NOTMUCH_STATUS_LAST_STATUS) { - fprintf (stderr, "Internal error: Failed to find document immediately after adding it.\n"); - exit (1); + INTERNAL_ERROR ("Failed to find document immediately after adding it.\n"); } *status = (notmuch_status_t) private_status; @@ -218,9 +217,8 @@ notmuch_message_get_message_id (notmuch_message_t *message) i.skip_to (_find_prefix ("id")); if (i == message->doc.termlist_end ()) { - fprintf (stderr, "Internal error: Message with document ID of %d has no message ID.\n", - message->doc_id); - exit (1); + INTERNAL_ERROR ("Message with document ID of %d has no message ID.\n", + message->doc_id); } message->message_id = talloc_strdup (message, (*i).c_str () + 1); @@ -468,9 +466,8 @@ notmuch_message_add_tag (notmuch_message_t *message, const char *tag) status = _notmuch_message_add_term (message, "tag", tag); if (status) { - fprintf (stderr, "Internal error: _notmuch_message_add_term return unexpected value: %d\n", - status); - exit (1); + INTERNAL_ERROR ("_notmuch_message_add_term return unexpected value: %d\n", + status); } _notmuch_message_sync (message); @@ -491,9 +488,8 @@ notmuch_message_remove_tag (notmuch_message_t *message, const char *tag) status = _notmuch_message_remove_term (message, "tag", tag); if (status) { - fprintf (stderr, "Internal error: _notmuch_message_remove_term return unexpected value: %d\n", - status); - exit (1); + INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n", + status); } _notmuch_message_sync (message); diff --git a/notmuch-private.h b/notmuch-private.h index 53ea75fa..53b4f1dd 100644 --- a/notmuch-private.h +++ b/notmuch-private.h @@ -48,6 +48,20 @@ NOTMUCH_BEGIN_DECLS #define COMPILE_TIME_ASSERT(pred) ((void)sizeof(char[1 - 2*!(pred)])) +/* There's no point in continuing when we've detected that we've done + * something wrong internally (as opposed to the user passing in a + * bogus value). + * + * Note that __location__ comes from talloc.h. + */ +#define INTERNAL_ERROR(format, ...) \ + do { \ + fprintf(stderr, \ + "Internal error: " format " (%s)\n", \ + ##__VA_ARGS__, __location__); \ + exit (1); \ + } while (0) + /* Thanks to Andrew Tridgell's (SAMBA's) talloc for this definition of * unlikely. The talloc source code comes to us via the GNU LGPL v. 3. */ diff --git a/notmuch.c b/notmuch.c index 46c3c63b..c5fef0e8 100644 --- a/notmuch.c +++ b/notmuch.c @@ -43,6 +43,20 @@ #include /* g_strdup_printf */ +/* There's no point in continuing when we've detected that we've done + * something wrong internally (as opposed to the user passing in a + * bogus value). + * + * Note that __location__ comes from talloc.h. + */ +#define INTERNAL_ERROR(format, ...) \ + do { \ + fprintf(stderr, \ + "Internal error: " format " (%s)\n", \ + ##__VA_ARGS__, __location__); \ + exit (1); \ + } while (0) + #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0])) typedef int (*command_function_t) (int argc, char *argv[]); @@ -255,8 +269,7 @@ add_files_recursive (notmuch_database_t *notmuch, ret = status; goto DONE; default: - fprintf (stderr, "Internal error: add_message returned unexpected value: %d\n", status); - ret = status; + INTERNAL_ERROR ("add_message returned unexpected value: %d", status); goto DONE; } if (state->processed_files % 1000 == 0) diff --git a/query.cc b/query.cc index f5ee7aad..88d76ef1 100644 --- a/query.cc +++ b/query.cc @@ -178,8 +178,7 @@ notmuch_results_get (notmuch_results_t *results) if (message == NULL && status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) { - fprintf (stderr, "Internal error: a results iterator contains a non-existent document ID.\n"); - exit (1); + INTERNAL_ERROR ("a results iterator contains a non-existent document ID.\n"); } return message; diff --git a/xutil.c b/xutil.c index eadd3783..26761d78 100644 --- a/xutil.c +++ b/xutil.c @@ -104,10 +104,8 @@ xregcomp (regex_t *preg, const char *regex, int cflags) char *error = xmalloc (error_size); regerror (rerr, preg, error, error_size); - fprintf (stderr, "Internal error compiling regex %s: %s\n", - regex, error); - free (error); - exit (1); + INTERNAL_ERROR ("compiling regex %s: %s\n", + regex, error); } } @@ -122,11 +120,9 @@ xregexec (const regex_t *preg, const char *string, return rerr; for (i = 0; i < nmatch; i++) { - if (pmatch[i].rm_so == -1) { - fprintf (stderr, "Internal error matching regex against %s: Sub-match %d not found\n", - string, i); - exit (1); - } + if (pmatch[i].rm_so == -1) + INTERNAL_ERROR ("matching regex against %s: Sub-match %d not found\n", + string, i); } return 0;