]> git.notmuchmail.org Git - notmuch/commitdiff
Add an INTERNAL_ERROR macro and use it for all internal errors.
authorCarl Worth <cworth@cworth.org>
Sun, 25 Oct 2009 17:22:41 +0000 (10:22 -0700)
committerCarl Worth <cworth@cworth.org>
Sun, 25 Oct 2009 17:54:49 +0000 (10:54 -0700)
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.

database.cc
message-file.c
message.cc
notmuch-private.h
notmuch.c
query.cc
xutil.c

index 3f8ea903c7f7cdf33038a3f0c9b43ffc50942be5..3f0202277969d2289d192c61abc461379150af4e 100644 (file)
@@ -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 "";
 }
index fd7e68b48ed8d276890adac7eb8a31fedeb5120b..cb2bf665006c4de4734f446e28c782c08a0f8a8a 100644 (file)
@@ -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;
index b66ca7e2dc3cd7fdeb8a317084f360bd963dc34d..7a6c01d2b7ace590b9fde3a93b90d65c8f934a06 100644 (file)
@@ -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);
index 53ea75fa4d1c7bb09f2f08af50afd821b3ab0c5d..53b4f1ddbc38882e8c29adc468cc9cde57e3eb85 100644 (file)
@@ -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.
  */
index 46c3c63b525f626e18802772a8f0d27dd30f98f9..c5fef0e800961782a1941d2999a4be6f4c6146c0 100644 (file)
--- a/notmuch.c
+++ b/notmuch.c
 
 #include <glib.h> /* 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)
index f5ee7aadc5ee7185037fce302c805ba1592c1f86..88d76ef19ff6c2b825d81bbb256e218607c6f839 100644 (file)
--- 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 eadd3783f9477735d43cabd84f96c9a53e900184..26761d78e8914511a148a5fc2197175907ce2f69 100644 (file)
--- 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;