Re: [PATCH 2/2] lib: introduce notmuch_database_new for initializing a database handle

Subject: Re: [PATCH 2/2] lib: introduce notmuch_database_new for initializing a database handle

Date: Tue, 14 Jan 2014 09:22:06 -0400

To: Jani Nikula, Austin Clements

Cc: notmuch@notmuchmail.org

From: David Bremner


Jani Nikula <jani@nikula.org> writes:
>> Austin Wrote:
>>
>> Orthogonally -- and this may be a complete pipe dream of mine -- if we
>> just had a way to return more detailed error information than a simple
>> error code from notmuch_database_{create,open}, I think we wouldn't
>> need any of this.  Everything that these functions currently log
>> (modulo one warning) is error details, so if we could return the error
>> details *with the error* or somehow make them accessible, we wouldn't
>> need a logger at this point (or at several other points in the
>> library).
>
> Agreed. I tried to look into this earlier, but was hitting dead ends
> *if* we want to keep reporting user friendly error status in
> open/create. Obviously any concrete suggestions would be most welcome!
>

I'm not sure if this is also a dead end, but I was trying to sketch out
an api that returned something more detailed as status and came up with
the following.  The general idea is to replace notmuch_status_t with a
pointer to struct.  This will require pretty noisy source changes,
unless we're comfortable with using NULL pointer to indicate success.
In either case we'd rename the existing enum to something like
notmuch_status_code_t.

/* pseudo-C follows */

typedef struct notmuch_status_struct * notmuch_status_t;

/* we can just tell external users to pass NULL as the first argument */

notmuch_status_t notmuch_status_new (void *ctx, size_t bufsiz);
void notmuch_error_destroy (notuch_error_desc_t *victim);

/* printf equivalent */
notmuch_status_t *notmuch_status_format(notmuch_status_t dest,
					notmuch_status_code_t code,
					const char *format, ...);
/* case 1, caller allocates */
status = notmuch_status_new (BUFSIZ);
if (!status) {
    halt_and_catch_fire();
}

/* open could continue to return notmuch_status_code_t, or just 0/1 */
if (notmuch_database_open (notmuch_config_get_database_path (config),
			   NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch, status)) {
    fprintf (stderr, "oops: %s\n", notmuch_status_to_string (status));
    notmuch_error_destroy(error_details);
    return 1;
}

/* case 2, callee allocates */

status = notmuch_message_freeze (message);
if (notmuch_status_to_code (status)) {  /* every check needs to be changed, unless NULL=OK */
    message_error (message, status, "freezing message");
    return status;
}

/* some existing code is left alone */

fprintf (stderr, "Message-ID: %s\n", notmuch_message_get_message_id (message));
fprintf (stderr, "Status: %s\n", notmuch_status_to_string (status));


Thread: