This checks for non-zero exit status and pops up an error buffer
giving the exit status, the stderr, and the stdout.  Schema version
mismatches are handled specially.
---
 emacs/notmuch-lib.el |   40 +++++++++++++++++++++++++++++++++-------
 1 file changed, 33 insertions(+), 7 deletions(-)

diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el
index 31c69dc..9c6be13 100644
--- a/emacs/notmuch-lib.el
+++ b/emacs/notmuch-lib.el
@@ -361,15 +361,41 @@ You may need to restart Emacs or upgrade your notmuch 
package."))
   "Invoke `notmuch-command' with `args' and return the parsed JSON output.

 The returned output will represent objects using property lists
-and arrays as lists."
+and arrays as lists.  If notmuch exits with a non-zero status,
+this will pop up a buffer containing notmuch's output and signal
+an error."

   (with-temp-buffer
-    (apply #'call-process notmuch-command nil (list t nil) nil args)
-    (goto-char (point-min))
-    (let ((json-object-type 'plist)
-         (json-array-type 'list)
-         (json-false 'nil))
-      (json-read))))
+    (let ((err-file (make-temp-file "nmerr")))
+      (unwind-protect
+         (let ((status (apply #'call-process
+                              notmuch-command nil (list t err-file) nil args)))
+           (cond ((equal status 0)
+                  (goto-char (point-min))
+                  (let ((json-object-type 'plist)
+                        (json-array-type 'list)
+                        (json-false 'nil))
+                    (json-read)))
+                 ((or (equal status 20) (equal status 21))
+                  ;; Special handling of version mismatch errors
+                  (notmuch-version-mismatch-error status))
+                 (t
+                  (goto-char (point-min))
+                  (insert "Error:\n")
+                  (let ((pos (point)))
+                    (insert-file-contents err-file)
+                    (when (= (point) pos)
+                      (insert "(no error output)\n")))
+                  (unless (eobp)
+                    (insert "Output:\n"))
+                  (notmuch-pop-up-error
+                   (concat
+                    (format "CLI error.  %S exited with %s%s.\n"
+                            (cons notmuch-command args)
+                            (if (integerp status) "status " "")
+                            status)
+                    (buffer-string))))))
+       (delete-file err-file)))))

 ;; Compatibility functions for versions of emacs before emacs 23.
 ;;
-- 
1.7.10.4

Reply via email to