Re: [PATCH] emacs: Use the minibuffer for CLI error reporting
authorAustin Clements <amdragon@MIT.EDU>
Thu, 3 Jan 2013 00:44:16 +0000 (19:44 +1900)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:52:54 +0000 (09:52 -0800)
6b/a717938896a4a59079b7c4ce0042315d2a1d15 [new file with mode: 0644]

diff --git a/6b/a717938896a4a59079b7c4ce0042315d2a1d15 b/6b/a717938896a4a59079b7c4ce0042315d2a1d15
new file mode 100644 (file)
index 0000000..5393865
--- /dev/null
@@ -0,0 +1,362 @@
+Return-Path: <amdragon@mit.edu>\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 1421C431FAF\r
+       for <notmuch@notmuchmail.org>; Wed,  2 Jan 2013 16:44:22 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+       tests=[RCVD_IN_DNSWL_LOW=-0.7] 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 9dSF8q5n+qsL for <notmuch@notmuchmail.org>;\r
+       Wed,  2 Jan 2013 16:44:21 -0800 (PST)\r
+Received: from dmz-mailsec-scanner-3.mit.edu (DMZ-MAILSEC-SCANNER-3.MIT.EDU\r
+       [18.9.25.14])\r
+       by olra.theworths.org (Postfix) with ESMTP id CEFF4431FAE\r
+       for <notmuch@notmuchmail.org>; Wed,  2 Jan 2013 16:44:20 -0800 (PST)\r
+X-AuditID: 1209190e-b7fa16d000001402-74-50e4d4632a33\r
+Received: from mailhub-auth-4.mit.edu ( [18.7.62.39])\r
+       by dmz-mailsec-scanner-3.mit.edu (Symantec Messaging Gateway) with SMTP\r
+       id BA.40.05122.364D4E05; Wed,  2 Jan 2013 19:44:19 -0500 (EST)\r
+Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103])\r
+       by mailhub-auth-4.mit.edu (8.13.8/8.9.2) with ESMTP id r030iIHx023009; \r
+       Wed, 2 Jan 2013 19:44:18 -0500\r
+Received: from awakening.csail.mit.edu (awakening.csail.mit.edu [18.26.4.91])\r
+       (authenticated bits=0)\r
+       (User authenticated as amdragon@ATHENA.MIT.EDU)\r
+       by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id r030iGKR005149\r
+       (version=TLSv1/SSLv3 cipher=DHE-RSA-AES128-SHA bits=128 verify=NOT);\r
+       Wed, 2 Jan 2013 19:44:17 -0500 (EST)\r
+Received: from amthrax by awakening.csail.mit.edu with local (Exim 4.80)\r
+       (envelope-from <amdragon@mit.edu>)\r
+       id 1TqYuu-0001T1-7V; Wed, 02 Jan 2013 19:44:16 -0500\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: Mark Walters <markwalters1009@gmail.com>, notmuch@notmuchmail.org\r
+Subject: Re: [PATCH] emacs: Use the minibuffer for CLI error reporting\r
+In-Reply-To: <87r4m820yh.fsf@qmul.ac.uk>\r
+References: <87wqw2pcqs.fsf@zancas.localnet>\r
+       <1356724088-26032-1-git-send-email-amdragon@mit.edu>\r
+       <87r4m820yh.fsf@qmul.ac.uk>\r
+User-Agent: Notmuch/0.14+236~gf64406d (http://notmuchmail.org) Emacs/23.4.1\r
+       (i486-pc-linux-gnu)\r
+Date: Wed, 02 Jan 2013 19:44:16 -0500\r
+Message-ID: <874nizksdb.fsf@awakening.csail.mit.edu>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=us-ascii\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFupileLIzCtJLcpLzFFi42IRYrdT102+8iTA4M8seYsbrd2MFnv2eVms\r
+       nstjcf3mTGYHFo+7p7k8ds66y+7xbNUtZo8th94zB7BEcdmkpOZklqUW6dslcGWcW/yYqeBV\r
+       TMXF/edZGhhne3QxcnJICJhITH3eywhhi0lcuLeerYuRi0NIYB+jROfOuVDOekaJZSemQzkX\r
+       mCR+nfrCBOEsYZToPn+HGaSfTUBDYtv+5WCzRARcJZ5++wwWZxYwlNgy7S07iC0s4Cax6NFq\r
+       sBpOoPqbuxrA4kICtRLbuj+zgtiiAvESz+99YwGxWQRUJV69fAK0jIODF+jWP+cCQcK8AoIS\r
+       J2c+YYEYryVx499LpgmMgrOQpGYhSS1gZFrFKJuSW6Wbm5iZU5yarFucnJiXl1qka6yXm1mi\r
+       l5pSuokRFMycknw7GL8eVDrEKMDBqMTDu6LmSYAQa2JZcWXuIUZJDiYlUd78i0AhvqT8lMqM\r
+       xOKM+KLSnNTiQ4wSHMxKIrzXc4ByvCmJlVWpRfkwKWkOFiVx3ispN/2FBNITS1KzU1MLUotg\r
+       sjIcHEoSvFMvAzUKFqWmp1akZeaUIKSZODhBhvMADa8BqeEtLkjMLc5Mh8ifYtTlaHh54ymj\r
+       EEtefl6qlDjvYpAiAZCijNI8uDmwJPSKURzoLWHeJpAqHmACg5v0CmgJE9CSV28egywpSURI\r
+       STUwysjX9D5j95gbsHLukdK7O4K4DivuP805jdNF+oTxrAvH5/bs+zJTZ+b707udN7UVS81Y\r
+       G8h8cd2R/AXR17P/9J9U/huxU2j3vAUePn031RkWb9pr9yzZ5dzdt+vWTExl+ibUwmazPm/X\r
+       ked5i9wKnl1/+mW3RAmX0HHRWP0pjYt0OJRLT9e/Oq/EUpyRaKjFXFScCAAongfTHQMAAA==\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: Thu, 03 Jan 2013 00:44:22 -0000\r
+\r
+On Sat, 29 Dec 2012, Mark Walters <markwalters1009@gmail.com> wrote:\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
+Done.\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
+Good point.  v2 basically follows your suggestion of removing the\r
+`when'.\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 removing the `when' simplifies this enough.  I'd rather not lift\r
+the let outside the cond because the process of getting the error\r
+message is nontrivial and would be a waste in the common case of a\r
+success exit 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
+I'm not too worried about always having the reference to the errors\r
+buffer.  My hope is that most cases will provide an error file (search\r
+is a notable exception and may be worth fixing), in which case it's\r
+going to provide that reference regardless.\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
+Sounds like a good follow-up patch.  Though currently the code is full\r
+of direct call-process and process-lines calls, so it would take a\r
+little (worthwhile) effort to consolidate these.\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
+I'm not sure I can do much beyond what's in the patch.  v2 improves it a\r
+bit to be more thorough, so now both tests systematically collect the\r
+buffer, the errors buffer, and messages.  See what you think.\r
+\r
+Async errors are harder, since it's 2013 and Emacs still provides no\r
+means to separate stdout from stderr for async processes.  The official\r
+way to do this is to fire up a shell running the command and have the\r
+shell redirect stderr.  This may be worthwhile for search since it would\r
+give us better error messages and eliminate the crazy resynchronization\r
+we have to do to deal with errors embedded in the output, but that's for\r
+another patch.\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