[PATCH] WIP: pass through errors to structured output

David Bremner david at tethera.net
Thu Nov 9 17:14:33 PST 2017


---

like the subject says, work in progress.

The idea is to include an optional error reporting dictionary in
structured output to help clients figure out wth went wrong without
attempting to parse stderr. It's a new key so it shouldn't break sane
clients. Or even notmuch-emacs ;).

Some things to complete/bikeshed.

     - some of the names are clearly terrible. "message", I'm looking at you.
     - make most of the NULL returns here return a node with some
       error info.
     - possibly still print to stderr for non-structured formats.
     - maybe think about a future consolidation of all of the status info
     
     
 mime-node.c      | 13 +++++++++----
 notmuch-client.h |  4 ++++
 notmuch-show.c   | 10 ++++++++++
 3 files changed, 23 insertions(+), 4 deletions(-)

diff --git a/mime-node.c b/mime-node.c
index e1aca969..dafa8eb2 100644
--- a/mime-node.c
+++ b/mime-node.c
@@ -272,11 +272,16 @@ _mime_node_create (mime_node_t *parent, GMimeObject *part)
 	notmuch_status_t status;
 	status = _notmuch_crypto_get_gmime_ctx_for_protocol (node->ctx->crypto,
 							     protocol, &cryptoctx);
-	if (status) /* this is a warning, not an error */
-	    fprintf (stderr, "Warning: %s (%s).\n", notmuch_status_to_string (status),
+	if (status) {
+	    node->message = talloc_asprintf (node, "Warning: %s (%s).\n", notmuch_status_to_string (status),
 		     protocol ? protocol : "NULL");
-	if (!cryptoctx)
+	    node->status = status;
+	}
+	if (!cryptoctx) {
+
+
 	    return node;
+	}
     }
 #endif
 
@@ -300,7 +305,7 @@ _mime_node_create (mime_node_t *parent, GMimeObject *part)
 	    node_verify (node, part, cryptoctx);
 	}
     }
-
+    node->status = NOTMUCH_STATUS_SUCCESS;
     return node;
 }
 
diff --git a/notmuch-client.h b/notmuch-client.h
index f7524e59..45451ecb 100644
--- a/notmuch-client.h
+++ b/notmuch-client.h
@@ -410,6 +410,10 @@ struct mime_node {
      * number to assign it (or -1 if unknown). */
     int next_child;
     int next_part_num;
+
+    /* for error / warning reporting */
+    notmuch_status_t status;
+    char *message;
 };
 
 /* Construct a new MIME node pointing to the root message part of
diff --git a/notmuch-show.c b/notmuch-show.c
index 7afd3947..00108174 100644
--- a/notmuch-show.c
+++ b/notmuch-show.c
@@ -644,6 +644,16 @@ format_part_sprinter (const void *ctx, sprinter_t *sp, mime_node_t *node,
     sp->map_key (sp, "id");
     sp->integer (sp, node->part_num);
 
+    if (node->status != NOTMUCH_STATUS_SUCCESS) {
+	sp->map_key (sp, "error_info");
+	sp->begin_map (sp);
+	sp->map_key (sp, "status");
+	sp->integer (sp, node->status);
+	sp->map_key (sp, "message");
+	sp->string (sp, node->message);
+	sp->end (sp);
+    }
+
     if (node->decrypt_attempted) {
 	sp->map_key (sp, "encstatus");
 	sp->begin_list (sp);
-- 
2.14.2



More information about the notmuch mailing list