Subject: bug#75065: closed (Re: bug#75065: Upon archive download failure print the original error)

From: [email-masked] (GNU bug Tracking System)
Date: Fri, 27 Dec 2024 17:02:02 +0000

Your bug report

#75065: Upon archive download failure print the original error

which was filed against the emacs package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
75065: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=75065
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: Re: bug#75065: Upon archive download failure print the original error
From: Stefan Monnier <[email-masked]>
To: Philip Kaludercic <[email-masked]>
Cc: [email-masked], Eli Zaretskii <[email-masked]>, Konstantin Kharlamov <[email-masked]>
Date: Fri, 27 Dec 2024 12:01:14 -0500
List-Id: <debbugs-submit.debbugs.gnu.org>

> `%s` to print `err` or `(cdr err)` would be wrong, since `%s` is for use
> with strings rather than lists.  IOW, IMO, it should be either
>
>     ...%S" ... err)
>
> or
>
>     ...%s" ... (error-message-string err))
>
> where the first is a bit more "debugging/developer" friendly and the second
> is a bit more "user" friendly.

Revising the code of `package.el` I see that those errors&messages are
meant to be exposed to the user, so they should preferably use
`error-message-string`.

So I installed the patch below into `master`.
Thank you!
Closing,

        Stefan
commit a99e1cc745047977b0b7cbd25fe9df66e9c86a39
Author: Stefan Monnier <[email-masked]>
Date:   Fri Dec 27 11:58:30 2024 -0500

    (package--download-and-read-archives): Fix bug#75065
    
    * lisp/emacs-lisp/package.el (package--download-and-read-archives):
    Include the error info in the error message.
    Suggested by Konstantin Kharlamov <[email-masked]>.
    (package-refresh-contents, package-menu--perform-transaction):
    Use `error-message-string`.

diff --git a/lisp/emacs-lisp/package.el b/lisp/emacs-lisp/package.el
index 5f785071ea3..bff45d57b07 100644
--- a/lisp/emacs-lisp/package.el
+++ b/lisp/emacs-lisp/package.el
@@ -1829,10 +1829,11 @@ package--download-and-read-archives
 If optional argument ASYNC is non-nil, perform the downloads
 asynchronously."
   (dolist (archive package-archives)
-    (condition-case-unless-debug nil
+    (condition-case-unless-debug err
         (package--download-one-archive archive "archive-contents" async)
-      (error (message "Failed to download `%s' archive."
-               (car archive))))))
+      (error (message "Failed to download `%s' archive: %s"
+                      (car archive)
+                      (error-message-string err))))))
 
 (defvar package-refresh-contents-hook (list #'package--download-and-read-archives)
   "List of functions to call to refresh the package archive.
@@ -1856,7 +1857,8 @@ package-refresh-contents
     (when (and (package-check-signature) (file-exists-p default-keyring))
       (condition-case-unless-debug error
           (package-import-keyring default-keyring)
-        (error (message "Cannot import default keyring: %S" (cdr error))))))
+        (error (message "Cannot import default keyring: %s"
+                        (error-message-string error))))))
   (run-hook-with-args 'package-refresh-contents-hook async))
 
 
@@ -3989,8 +3991,9 @@ package-menu--perform-transaction
               (package-delete elt nil 'nosave))
           (error
            (push (package-desc-full-name elt) errors)
-           (message "Error trying to delete `%s': %S"
-                    (package-desc-full-name elt) err)))))
+           (message "Error trying to delete `%s': %s"
+                    (package-desc-full-name elt)
+                    (error-message-string err))))))
     errors))
 
 (defun package--update-selected-packages (add remove)

Subject: Upon archive download failure print the original error
From: Konstantin Kharlamov <[email-masked]>
To: [email-masked]
Date: Tue, 24 Dec 2024 18:25:30 +0300
List-Id: <debbugs-submit.debbugs.gnu.org>

I was recently helping out a new Emacs user with package installation¹,
and I found an interesting thing: if you put to `package-archives` a
URL without `https` prefix, download will fail. Long story short, the
reason turns out that `package-archives` also supports local paths,
which the URL being considered as. However, Emacs never prints a
message about that, even though such message exists in the code.
Instead it just says that download failed, leaving a user wondering
why.

That happens because (package--download-and-read-archives) ignores the
exception message, and always just prints generic failure message.

This code fixes this, so now the actual failure message will be
correctly shown.

1:
https://emacs.stackexchange.com/questions/82828/is-installing-deadgrep-fron-melpa-still-possible/82829#82829
From fb4685238726a79599f6388318916d2962da93ae Mon Sep 17 00:00:00 2001
From: Konstantin Kharlamov <[email-masked]>
Date: Tue, 24 Dec 2024 18:16:41 +0300
Subject: [PATCH] Upon archive download failure print the original error

* lisp/emacs-lisp/package.el (package--download-and-read-archives):
upon catching exception, use the exception message as part of the
error to provide more context about the failure.
---
 lisp/emacs-lisp/package.el | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/lisp/emacs-lisp/package.el b/lisp/emacs-lisp/package.el
index 5f785071ea3..cb81efc71f0 100644
--- a/lisp/emacs-lisp/package.el
+++ b/lisp/emacs-lisp/package.el
@@ -1829,10 +1829,10 @@ Populate `package-archive-contents' with the result.
 If optional argument ASYNC is non-nil, perform the downloads
 asynchronously."
   (dolist (archive package-archives)
-    (condition-case-unless-debug nil
+    (condition-case-unless-debug err
         (package--download-one-archive archive "archive-contents" async)
-      (error (message "Failed to download `%s' archive."
-               (car archive))))))
+      (error (message "Failed to download `%s' archive. Error: %S"
+               (car archive) (cdr err))))))
 
 (defvar package-refresh-contents-hook (list #'package--download-and-read-archives)
   "List of functions to call to refresh the package archive.
-- 
2.47.1