Re: [PATCH] emacs: Use the minibuffer for CLI error reporting
authorMark Walters <markwalters1009@gmail.com>
Sat, 29 Dec 2012 18:00:06 +0000 (18:00 +0000)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:52:52 +0000 (09:52 -0800)
d2/d3a9f15ca927e94f11c000765724cf0a1a8388 [new file with mode: 0644]

diff --git a/d2/d3a9f15ca927e94f11c000765724cf0a1a8388 b/d2/d3a9f15ca927e94f11c000765724cf0a1a8388
new file mode 100644 (file)
index 0000000..2e31fb7
--- /dev/null
@@ -0,0 +1,329 @@
+Return-Path: <m.walters@qmul.ac.uk>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id 78E08431FB6\r
+       for <notmuch@notmuchmail.org>; Sat, 29 Dec 2012 10:00:20 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -1.098\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-1.098 tagged_above=-999 required=5\r
+       tests=[DKIM_ADSP_CUSTOM_MED=0.001, FREEMAIL_FROM=0.001,\r
+       NML_ADSP_CUSTOM_MED=1.2, RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+       by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+       with ESMTP id Qk6IuyG2vCsj for <notmuch@notmuchmail.org>;\r
+       Sat, 29 Dec 2012 10:00:19 -0800 (PST)\r
+Received: from mail2.qmul.ac.uk (mail2.qmul.ac.uk [138.37.6.6])\r
+       (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id A3780431FAF\r
+       for <notmuch@notmuchmail.org>; Sat, 29 Dec 2012 10:00:18 -0800 (PST)\r
+Received: from smtp.qmul.ac.uk ([138.37.6.40])\r
+       by mail2.qmul.ac.uk with esmtp (Exim 4.71)\r
+       (envelope-from <m.walters@qmul.ac.uk>)\r
+       id 1Tp0hj-00031n-B4; Sat, 29 Dec 2012 18:00:15 +0000\r
+Received: from 94.197.9.104.threembb.co.uk ([94.197.9.104] helo=localhost)\r
+       by smtp.qmul.ac.uk with esmtpsa (TLSv1:AES128-SHA:128) (Exim 4.69)\r
+       (envelope-from <m.walters@qmul.ac.uk>)\r
+       id 1Tp0hc-00021Y-9f; Sat, 29 Dec 2012 18:00:15 +0000\r
+From: Mark Walters <markwalters1009@gmail.com>\r
+To: Austin Clements <amdragon@MIT.EDU>, notmuch@notmuchmail.org\r
+Subject: Re: [PATCH] emacs: Use the minibuffer for CLI error reporting\r
+In-Reply-To: <1356724088-26032-1-git-send-email-amdragon@mit.edu>\r
+References: <87wqw2pcqs.fsf@zancas.localnet>\r
+       <1356724088-26032-1-git-send-email-amdragon@mit.edu>\r
+User-Agent: Notmuch/0.14+236~g1d0044f (http://notmuchmail.org) Emacs/23.4.1\r
+       (x86_64-pc-linux-gnu)\r
+Date: Sat, 29 Dec 2012 18:00:06 +0000\r
+Message-ID: <87r4m820yh.fsf@qmul.ac.uk>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=us-ascii\r
+X-Sender-Host-Address: 94.197.9.104\r
+X-QM-SPAM-Info: Sender has good ham record.  :)\r
+X-QM-Body-MD5: b41ddfec0532531c5a658b929c786d0a (of first 20000 bytes)\r
+X-SpamAssassin-Score: -1.8\r
+X-SpamAssassin-SpamBar: -\r
+X-SpamAssassin-Report: The QM spam filters have analysed this message to\r
+       determine if it is\r
+       spam. We require at least 5.0 points to mark a message as spam.\r
+       This message scored -1.8 points.\r
+       Summary of the scoring: \r
+       * -2.3 RCVD_IN_DNSWL_MED RBL: Sender listed at http://www.dnswl.org/,\r
+       *      medium trust\r
+       *      [138.37.6.40 listed in list.dnswl.org]\r
+       * 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail\r
+       provider *      (markwalters1009[at]gmail.com)\r
+       * -0.0 T_RP_MATCHES_RCVD Envelope sender domain matches handover relay\r
+       *      domain\r
+       *  0.5 AWL AWL: From: address is in the auto white-list\r
+X-QM-Scan-Virus: ClamAV says the message is clean\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+       <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Sat, 29 Dec 2012 18:00:20 -0000\r
+\r
+On Fri, 28 Dec 2012, Austin Clements <amdragon@MIT.EDU> wrote:\r
+> We recently switched to popping up a buffer to report CLI errors, but\r
+> this was too intrusive, especially for transient errors and especially\r
+> since we made fewer things ignore errors.  This patch changes this to\r
+> display a basic error message in the minibuffer (using Emacs' usual\r
+> error handling path) and, if there are additional details, to log\r
+> these to a separate error buffer and reference the error buffer from\r
+> the minibuffer message.  This is more in line with how Emacs typically\r
+> handles errors, but makes the details available to the user without\r
+> flooding them with the details.\r
+>\r
+> Given this split, we pare down the basic message and make it more\r
+> user-friendly, and also make the verbose message even more detailed\r
+> (and more debugging-oriented).\r
+\r
+I like this approach but have some queries below.\r
+\r
+> ---\r
+>  emacs/notmuch-lib.el |   92 ++++++++++++++++++++++++++++----------------------\r
+>  emacs/notmuch.el     |    9 +++--\r
+>  test/emacs           |   11 +++---\r
+>  test/emacs-show      |    6 ++--\r
+>  4 files changed, 67 insertions(+), 51 deletions(-)\r
+>\r
+> diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el\r
+> index 77a591d..3baab97 100644\r
+> --- a/emacs/notmuch-lib.el\r
+> +++ b/emacs/notmuch-lib.el\r
+> @@ -316,23 +316,28 @@ string), a property list of face attributes, or a list of these."\r
+>      (put-text-property pos next 'face (cons face cur))\r
+>      (setq pos next)))))\r
+>  \r
+> -(defun notmuch-pop-up-error (msg)\r
+> -  "Pop up an error buffer displaying MSG.\r
+> -\r
+> -This will accumulate error messages in the errors buffer until\r
+> -the user dismisses it."\r
+> -\r
+> -  (let ((buf (get-buffer-create "*Notmuch errors*")))\r
+> -    (with-current-buffer buf\r
+> -      (view-mode-enter nil #'kill-buffer)\r
+> -      (let ((inhibit-read-only t))\r
+> -    (goto-char (point-max))\r
+> -    (unless (bobp)\r
+> -      (insert "\n"))\r
+> -    (insert msg)\r
+> +(defun notmuch-logged-error (msg &optional extra)\r
+> +  "Log MSG and EXTRA to *Notmuch errors* and signal MSG.\r
+> +\r
+> +This logs MSG and EXTRA to the *Notmuch errors* buffer and\r
+> +signals MSG as an error.  If EXTRA is non-nil, text referring the\r
+> +user to the *Notmuch errors* buffer will be appended to the\r
+> +signaled error."\r
+\r
+It might be worth commenting that since this signals an error it does\r
+not "return"; I found the code in notmuch-check-exit-status rather\r
+confusing until I realised that.\r
+\r
+> +\r
+> +  (with-current-buffer (get-buffer-create "*Notmuch errors*")\r
+> +    (goto-char (point-max))\r
+> +    (unless (bobp)\r
+> +      (newline))\r
+> +    (save-excursion\r
+> +      (insert "[" (current-time-string) "]\n" msg)\r
+> +      (unless (bolp)\r
+> +    (newline))\r
+> +      (when extra\r
+> +    (insert extra)\r
+>      (unless (bolp)\r
+> -      (insert "\n"))))\r
+> -    (pop-to-buffer buf)))\r
+> +      (newline)))))\r
+> +  (error "%s" (concat msg (when extra\r
+> +                        " (see *Notmuch errors* for more details)"))))\r
+>  \r
+>  (defun notmuch-check-async-exit-status (proc msg)\r
+>    "If PROC exited abnormally, pop up an error buffer and signal an error.\r
+> @@ -363,35 +368,40 @@ contents of ERR-FILE will be included in the error message."\r
+>    (cond\r
+>     ((eq exit-status 0) t)\r
+>     ((eq exit-status 20)\r
+> -    (notmuch-pop-up-error "Error: Version mismatch.\r
+> +    (notmuch-logged-error "notmuch CLI version mismatch\r
+>  Emacs requested an older output format than supported by the notmuch CLI.\r
+> -You may need to restart Emacs or upgrade your notmuch Emacs package.")\r
+> -    (error "notmuch CLI version mismatch"))\r
+> +You may need to restart Emacs or upgrade your notmuch Emacs package."))\r
+>     ((eq exit-status 21)\r
+> -    (notmuch-pop-up-error "Error: Version mismatch.\r
+> +    (notmuch-logged-error "notmuch CLI version mismatch\r
+>  Emacs requested a newer output format than supported by the notmuch CLI.\r
+> -You may need to restart Emacs or upgrade your notmuch package.")\r
+> -    (error "notmuch CLI version mismatch"))\r
+> +You may need to restart Emacs or upgrade your notmuch package."))\r
+>     (t\r
+> -    (notmuch-pop-up-error\r
+> -     (concat\r
+> -      (format "Error invoking notmuch.  %s exited with %s%s.\n"\r
+> -          (mapconcat #'identity command " ")\r
+> -          ;; Signal strings look like "Terminated", hence the\r
+> -          ;; colon.\r
+> -          (if (integerp exit-status) "status " "signal: ")\r
+> -          exit-status)\r
+> -      (when err-file\r
+> -    (concat "Error:\n"\r
+> -            (with-temp-buffer\r
+> -              (insert-file-contents err-file)\r
+> -              (if (eobp)\r
+> -                  "(no error output)\n"\r
+> -                (buffer-string)))))\r
+> -      (when (and output (not (equal output "")))\r
+> -    (format "Output:\n%s" output))))\r
+> -    ;; Mimic `process-lines'\r
+> -    (error "%s exited with status %s" (car command) exit-status))))\r
+> +    (let ((err (when err-file\r
+> +             (with-temp-buffer\r
+> +               (insert-file-contents err-file)\r
+> +               (unless (eobp)\r
+> +                 (buffer-string)))))\r
+> +      (basic-msg (format "%s exited with status %s"\r
+> +                         (car command) exit-status)))\r
+> +      (when (and (null err) (or (null output) (equal output "")))\r
+> +    ;; We have no details to speak of.  Mimic `process-lines'.\r
+\r
+This means that if err and output are null we give a minimal error message and we\r
+don't log the command line that fails. Perhaps the `when' clause could\r
+be omitted so we get the extra information from below?\r
+\r
+> +    (notmuch-logged-error basic-msg))\r
+> +      (let ((extra\r
+> +         (concat\r
+> +          "Command: " (mapconcat #'shell-quote-argument command " ") "\n"\r
+> +          (if (integerp exit-status)\r
+> +              (format "Exit status: %s\n" exit-status)\r
+> +            (format "Exit signal: %s\n" exit-status))\r
+> +          "Output:\n"\r
+> +          (if (and output (not (equal output "")))\r
+> +              output\r
+> +            "(none)"))))\r
+> +    (if err\r
+> +        ;; We have an error message straight from the CLI.\r
+> +        (notmuch-logged-error err extra)\r
+> +      ;; We only have combined output from the CLI; don't inundate\r
+> +      ;; the user with it.\r
+> +      (notmuch-logged-error basic-msg extra)))))))\r
+\r
+Also, depending how the above gets changed, would it be worth pulling the let\r
+clause before the cond clause, and subsuming some of the when/if/else\r
+logic into the cond? This has the nice side effect that the reader\r
+expects cond clauses to stop after the first match so the fact that\r
+notmuch-check-exit-status signals an error would not matter when reading\r
+this code.\r
+\r
+I think a command line would be useful in almost all cases (in the error\r
+buffer). If you decide to always supply that then your error message\r
+might want tweaking as it would always have extra information in the\r
+error buffer.\r
+\r
+Finally, and this is only a thought, I wonder if the mechanism can be\r
+tweaked to provide debug information along these lines for all notmuch\r
+commands whether or not they succeed: something like if\r
+notmuch-debug-commands is set or there is an error? \r
+\r
+Incidentally do you have good ways to test this code (ie see what it\r
+does in each case)? My hackish experiments suggested the async errors\r
+were less useful than the sync ones but maybe that is just an inherent\r
+limitation of the emacs async mechanisms.\r
+\r
+\r
+Best wishes\r
+\r
+Mark\r
+\r
+\r
+\r
+\r
+\r
+\r
+>  \r
+>  (defun notmuch-call-notmuch-json (&rest args)\r
+>    "Invoke `notmuch-command' with `args' and return the parsed JSON output.\r
+> diff --git a/emacs/notmuch.el b/emacs/notmuch.el\r
+> index 63387a2..c98a4fe 100644\r
+> --- a/emacs/notmuch.el\r
+> +++ b/emacs/notmuch.el\r
+> @@ -654,11 +654,14 @@ of the result."\r
+>                  ;; showing the search buffer\r
+>                  (when (or (= exit-status 20) (= exit-status 21))\r
+>                    (kill-buffer))\r
+> -                (condition-case nil\r
+> +                (condition-case err\r
+>                      (notmuch-check-async-exit-status proc msg)\r
+>                    ;; Suppress the error signal since strange\r
+> -                  ;; things happen if a sentinel signals.\r
+> -                  (error (throw 'return nil)))\r
+> +                  ;; things happen if a sentinel signals.  Mimic\r
+> +                  ;; the top-level's handling of error messages.\r
+> +                  (error\r
+> +                   (message "%s" (second err))\r
+> +                   (throw 'return nil)))\r
+>                  (if (and atbob\r
+>                           (not (string= notmuch-search-target-thread "found")))\r
+>                      (set 'never-found-target-thread t)))))\r
+> diff --git a/test/emacs b/test/emacs\r
+> index 6b18968..8e0a4fd 100755\r
+> --- a/test/emacs\r
+> +++ b/test/emacs\r
+> @@ -862,18 +862,19 @@ exit 1\r
+>  EOF\r
+>  chmod a+x notmuch_fail\r
+>  test_emacs "(let ((notmuch-command \"$PWD/notmuch_fail\"))\r
+> +           (with-current-buffer \"*Messages*\" (erase-buffer))\r
+>             (notmuch-search \"tag:inbox\")\r
+>             (notmuch-test-wait)\r
+> -           (test-output)\r
+> -           (with-current-buffer \"*Notmuch errors*\"\r
+> -              (test-output \"ERROR\")))"\r
+> -test_expect_equal "$(cat OUTPUT ERROR)" "\\r
+> +           (with-current-buffer \"*Messages*\"\r
+> +              (test-output \"MESSAGES\"))\r
+> +           (test-output))"\r
+> +test_expect_equal "$(cat OUTPUT MESSAGES)" "\\r
+>  Error: Unexpected output from notmuch search:\r
+>  This is output\r
+>  Error: Unexpected output from notmuch search:\r
+>  This is an error\r
+>  End of search results.\r
+> -Error invoking notmuch.  $PWD/notmuch_fail search --format=json --format-version=1 --sort=newest-first tag:inbox exited with status 1."\r
+> +$PWD/notmuch_fail exited with status 1"\r
+>  \r
+>  \r
+>  test_done\r
+> diff --git a/test/emacs-show b/test/emacs-show\r
+> index ebf530b..ae9459d 100755\r
+> --- a/test/emacs-show\r
+> +++ b/test/emacs-show\r
+> @@ -177,10 +177,12 @@ test_emacs "(let ((notmuch-command \"$PWD/notmuch_fail\"))\r
+>             (test-output)\r
+>             (with-current-buffer \"*Notmuch errors*\"\r
+>                (test-output \"ERROR\")))"\r
+> +sed -i -e 's/^\[.*\]$/[XXX]/' ERROR\r
+>  test_expect_equal "$(cat OUTPUT ERROR)" "\\r
+> -Error invoking notmuch.  $PWD/notmuch_fail show --format=json --format-version=1 --exclude=false ' * ' exited with status 1.\r
+> -Error:\r
+> +[XXX]\r
+>  This is an error\r
+> +Command: $PWD/notmuch_fail show --format\\=json --format-version\\=1 --exclude\\=false \\' \\* \\'\r
+> +Exit status: 1\r
+>  Output:\r
+>  This is output"\r
+>  \r
+> -- \r
+> 1.7.10.4\r