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.
This commit is contained in:
Carl Worth 2009-10-25 10:22:41 -07:00
parent 3b8e3ab666
commit 7b227a6bf7
7 changed files with 48 additions and 35 deletions

View 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 "";
}

View 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"
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);
}
return NULL;

View 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",
INTERNAL_ERROR ("Message with document ID of %d has no message ID.\n",
message->doc_id);
exit (1);
}
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",
INTERNAL_ERROR ("_notmuch_message_add_term return unexpected value: %d\n",
status);
exit (1);
}
_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",
INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
status);
exit (1);
}
_notmuch_message_sync (message);

View 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.
*/

View file

@ -43,6 +43,20 @@
#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)

View file

@ -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;

10
xutil.c
View file

@ -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",
INTERNAL_ERROR ("compiling regex %s: %s\n",
regex, error);
free (error);
exit (1);
}
}
@ -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",
if (pmatch[i].rm_so == -1)
INTERNAL_ERROR ("matching regex against %s: Sub-match %d not found\n",
string, i);
exit (1);
}
}
return 0;