Improve error handling for package management

This commit is contained in:
Henrik Lissner 2017-05-19 22:29:47 +02:00
parent 0875aab7f0
commit 5e894027c5
2 changed files with 81 additions and 52 deletions

View file

@ -4,18 +4,29 @@
(defvar doom--last-refresh nil) (defvar doom--last-refresh nil)
;;;###autoload ;;;###autoload
(defun doom-refresh-packages () (defun doom-refresh-packages (&optional force-p)
"Refresh ELPA packages." "Refresh ELPA packages."
(doom-initialize) (doom-initialize)
(when (or force-p (getenv "DEBUG"))
(doom-refresh-clear-cache))
(let ((last-refresh (persistent-soft-fetch 'last-pkg-refresh "emacs"))) (let ((last-refresh (persistent-soft-fetch 'last-pkg-refresh "emacs")))
(when last-refresh (when last-refresh
(setq doom--last-refresh last-refresh))) (setq doom--last-refresh last-refresh)))
(when (or (not doom--last-refresh) (when (or (not doom--last-refresh)
(> (nth 1 (time-since doom--last-refresh)) 900)) (> (nth 1 (time-since doom--last-refresh)) 900))
(package-refresh-contents) (condition-case ex
(persistent-soft-store (progn
'last-pkg-refresh (setq doom--last-refresh (current-time)) (package-refresh-contents)
"emacs"))) (persistent-soft-store
'last-pkg-refresh (setq doom--last-refresh (current-time))
"emacs"))
('error
(doom-refresh-clear-cache)))))
;;;###autoload
(defun doom-refresh-clear-cache ()
"Clear the cache for `doom-refresh-packages'."
(persistent-soft-store 'last-pkg-refresh nil "emacs"))
;;;###autoload ;;;###autoload
(defun doom-package-backend (name) (defun doom-package-backend (name)
@ -157,6 +168,29 @@ Used by `doom/packages-install'."
nil t))) nil t)))
(cdr (assoc name table)))) (cdr (assoc name table))))
(defmacro doom--condition-case! (&rest body)
`(condition-case ex
(condition-case ex2
(progn ,@body)
('file-error
(message! (bold (red " Couldn't find %s\n Trying again..." (cdr ex))))
(doom-refresh-packages)
,@body))
('user-error
(message! (bold (red " ERROR: %s" ex))))
('error
(doom-refresh-clear-cache)
(message! (bold (red " FATAL ERROR: %s" ex)))
(when doom-debug-mode
(with-temp-buffer
(insert
(cl-loop for i from 2
for frame = (backtrace-frame i)
while frame
collect frame))
(indent-code-rigidly (point-min) (point-max) 4)
(message! "%s" (buffer-string)))))))
;; ;;
;; Main functions ;; Main functions
@ -242,23 +276,17 @@ appropriate."
(doom-refresh-packages) (doom-refresh-packages)
(dolist (pkg packages) (dolist (pkg packages)
(message! "Installing %s" (car pkg)) (message! "Installing %s" (car pkg))
(condition-case ex (doom--condition-case!
(progn (message! " %s%s"
(message! (cond ((package-installed-p (car pkg))
" %s%s" (dark (white "ALREADY INSTALLED")))
(cond ((package-installed-p (car pkg)) ((doom-install-package (car pkg) (cdr pkg))
(dark (white "ALREADY INSTALLED"))) (green "DONE"))
((doom-install-package (car pkg) (cdr pkg)) (t
(green "DONE")) (red "FAILED")))
(t (if (plist-member (cdr pkg) :pin)
(red "FAILED"))) (format " [pinned: %s]" (plist-get (cdr pkg) :pin))
(if (plist-member (cdr pkg) :pin) "")))))
(format " [pinned: %s]" (plist-get (cdr pkg) :pin))
"")))
('user-error
(message! (bold (red " ERROR: %s" ex ))))
('error
(message! (bold (red " FATAL ERROR: %s" ex )))))))
(message! (bold (green "Finished!"))) (message! (bold (green "Finished!")))
(doom/reload)))) (doom/reload))))
@ -267,6 +295,7 @@ appropriate."
(defun doom/packages-update () (defun doom/packages-update ()
"Interactive command for updating packages." "Interactive command for updating packages."
(interactive) (interactive)
(doom-refresh-packages)
(let ((packages (sort (doom-get-outdated-packages) #'doom--sort-alpha))) (let ((packages (sort (doom-get-outdated-packages) #'doom--sort-alpha)))
(cond ((not packages) (cond ((not packages)
(message! (green "Everything is up-to-date"))) (message! (green "Everything is up-to-date")))
@ -290,19 +319,14 @@ appropriate."
(message! (yellow "Aborted!"))) (message! (yellow "Aborted!")))
(t (t
(doom-refresh-packages)
(dolist (pkg packages) (dolist (pkg packages)
(message! "Updating %s" (car pkg)) (message! "Updating %s" (car pkg))
(condition-case ex (doom--condition-case!
(message! (message!
(let ((result (doom-update-package (car pkg)))) (let ((result (doom-update-package (car pkg))))
(color (if result 'green 'red) (color (if result 'green 'red)
" %s" " %s"
(if result "DONE" "FAILED")))) (if result "DONE" "FAILED"))))))
('user-error
(message! (bold (red " ERROR: %s" ex))))
('error
(message! (bold (red " FATAL ERROR: %s" ex))))))
(message! (bold (green "Finished!"))) (message! (bold (green "Finished!")))
(doom/reload))))) (doom/reload)))))
@ -329,17 +353,13 @@ appropriate."
(t (t
(dolist (pkg packages) (dolist (pkg packages)
(condition-case ex (doom--condition-case!
(message! (message!
(let ((result (doom-delete-package pkg t))) (let ((result (doom-delete-package pkg t)))
(color (if result 'green 'red) (color (if result 'green 'red)
"%s %s" "%s %s"
(if result "Removed" "Failed to remove") (if result "Removed" "Failed to remove")
pkg))) pkg)))))
('user-error
(message! (bold (red " ERROR: %s" ex))))
('error
(message! (bold (red " FATAL ERROR: %s" ex))))))
(message! (bold (green "Finished!"))) (message! (bold (green "Finished!")))
(doom/reload))))) (doom/reload)))))
@ -400,6 +420,14 @@ calls."
(message "Aborted"))) (message "Aborted")))
(message "%s is up-to-date" package)))) (message "%s is up-to-date" package))))
;;;###autoload
(defun doom/refresh-packages (&optional force-p)
"Synchronize package metadata with the sources in `package-archives'. If
FORCE-P (the universal argument) is set, ignore the cache."
(declare (interactive-only t))
(interactive "P")
(doom-refresh-packages t))
;;;###autoload ;;;###autoload
(defun doom/am-i-secure () (defun doom/am-i-secure ()
"Test to see if your root certificates are securely configured in emacs." "Test to see if your root certificates are securely configured in emacs."

View file

@ -13,9 +13,9 @@
;; Why all the trouble? Because: ;; Why all the trouble? Because:
;; 1. Scriptability: I live in the command line. I want a programmable ;; 1. Scriptability: I live in the command line. I want a programmable
;; alternative to `list-packages' for updating and installing packages. ;; alternative to `list-packages' for updating and installing packages.
;; 2. Flexibility: I want packages from sources other than ELPA. Like github or ;; 2. Flexibility: I want packages from sources other than ELPA. Primarily
;; the Emacs wiki, because certain plugins are out-of-date through official ;; github, because certain plugins are out-of-date through official channels,
;; channels, have changed hands, or simply aren't in any ELPA repo. ;; have changed hands, or simply aren't in any ELPA repo.
;; 3. Stability: I used Cask before this. It would error out with cyrptic errors ;; 3. Stability: I used Cask before this. It would error out with cyrptic errors
;; depending on the version of Emacs I used and the alignment of the planets. ;; depending on the version of Emacs I used and the alignment of the planets.
;; No more. ;; No more.
@ -30,11 +30,12 @@
;; Technically, package.el commands should still work. To be absolutely sure, ;; Technically, package.el commands should still work. To be absolutely sure,
;; use the doom alternatives: ;; use the doom alternatives:
;; ;;
;; + `package-install': `doom/install-package' ;; + `package-install': `doom/install-package'
;; + `package-reinstall': `doom/reinstall-package' ;; + `package-reinstall': `doom/reinstall-package'
;; + `package-delete': `doom/delete-package' ;; + `package-delete': `doom/delete-package'
;; + `package-update': `doom/update-package' ;; + `package-update': `doom/update-package'
;; + `package-autoremove': `doom/packages-autoremove' ;; + `package-autoremove': `doom/packages-autoremove'
;; + `package-refresh-contents': `doom/refresh-packages'
;; ;;
;; See core/autoload/packages.el for more functions. ;; See core/autoload/packages.el for more functions.