From c116e27a84972f9e12a869f2b4983bb84ee50b15 Mon Sep 17 00:00:00 2001 From: Damien Cassou Date: Sat, 18 Feb 2023 12:16:48 +0100 Subject: [PATCH] org.el: Avoid crash in `org-file-contents' in case of network failure * lisp/org.el (org-file-contents): Wrap the `url-retrieve-synchronously' call into a `condition-case' block to avoid throwing an error when NOERROR is non-nil. TINYCHANGE --- lisp/org.el | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/lisp/org.el b/lisp/org.el index b0a6d8ef3..a9fd76734 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -4561,21 +4561,25 @@ (defun org-file-contents (file &optional noerror nocache) (cache) (is-url (if (org--should-fetch-remote-resource-p file) - (with-current-buffer (url-retrieve-synchronously file) - (goto-char (point-min)) - ;; Move point to after the url-retrieve header. - (search-forward "\n\n" nil :move) - ;; Search for the success code only in the url-retrieve header. - (if (save-excursion - (re-search-backward "HTTP.*\\s-+200\\s-OK" nil :noerror)) - ;; Update the cache `org--file-cache' and return contents. - (puthash file - (buffer-substring-no-properties (point) (point-max)) - org--file-cache) - (funcall (if noerror #'message #'user-error) - "Unable to fetch file from %S" - file) - nil)) + (condition-case error + (with-current-buffer (url-retrieve-synchronously file) + (goto-char (point-min)) + ;; Move point to after the url-retrieve header. + (search-forward "\n\n" nil :move) + ;; Search for the success code only in the url-retrieve header. + (if (save-excursion + (re-search-backward "HTTP.*\\s-+200\\s-OK" nil :noerror)) + ;; Update the cache `org--file-cache' and return contents. + (puthash file + (buffer-substring-no-properties (point) (point-max)) + org--file-cache) + (funcall (if noerror #'message #'user-error) + "Unable to fetch file from %S" + file) + nil)) + (error (if noerror + (message "Org could't download \"%s\": %s %S" file (car error) (cdr error)) + (signal (car error) (cdr error))))) (funcall (if noerror #'message #'user-error) "The remote resource %S is considered unsafe, and will not be downloaded." file))) -- 2.38.3