aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lisp/mastodon-media.el15
-rw-r--r--test/mastodon-media-tests.el18
2 files changed, 30 insertions, 3 deletions
diff --git a/lisp/mastodon-media.el b/lisp/mastodon-media.el
index 0a4c3de..50f79dd 100644
--- a/lisp/mastodon-media.el
+++ b/lisp/mastodon-media.el
@@ -170,9 +170,18 @@ MEDIA-TYPE is a symbol and either 'avatar or 'media-link."
`(:height ,mastodon-media--avatar-height))
((eq media-type 'media-link)
`(:max-height ,mastodon-media--preview-max-height))))))
- (url-retrieve url
- #'mastodon-media--process-image-response
- (list (copy-marker start) image-options region-length))))
+ (let ((buffer (current-buffer))
+ (marker (copy-marker start)))
+ (condition-case nil
+ ;; catch any errors in url-retrieve so as to not abort
+ ;; whatever called us
+ (url-retrieve url
+ #'mastodon-media--process-image-response
+ (list marker image-options region-length))
+ (error (with-current-buffer buffer
+ ;; TODO: Consider adding retries
+ (put-text-property marker (+ marker region-length) 'media-state 'loading-failed)
+ :loading-failed))))))
(defun mastodon-media--select-next-media-line ()
"Find coordinates of the next media to load.
diff --git a/test/mastodon-media-tests.el b/test/mastodon-media-tests.el
index 4bb89c7..7031e90 100644
--- a/test/mastodon-media-tests.el
+++ b/test/mastodon-media-tests.el
@@ -111,6 +111,24 @@
(let ((mastodon-media--preview-max-height 321))
(should (eq :called-as-expected (mastodon-media--load-image-from-url url 'media-link 7 5))))))))
+(ert-deftest mastodon-media:load-image-from-url:url-fetching-fails ()
+ "Should cope with failures in url-retrieve."
+ (let ((url "http://example.org/image.png")
+ (mastodon-media--avatar-height 123))
+ (with-mock
+ (mock (image-type-available-p 'imagemagick) => t)
+ (mock (create-image * 'imagemagick t :height 123) => '(image foo))
+ (stub url-retrieve => (error "url-retrieve failed"))
+
+ (with-temp-buffer
+ (insert (concat "Start:"
+ (mastodon-media--get-avatar-rendering "http://example.org/img.png")
+ ":rest"))
+
+ (should (eq :loading-failed (mastodon-media--load-image-from-url url 'avatar 7 1)))
+ ;; the media state was updated so we won't load this again:
+ (should (eq 'loading-failed (get-text-property 7 'media-state)))))))
+
(ert-deftest mastodon-media:process-image-response ()
"Should process the HTTP response and adjust the source buffer."
(with-temp-buffer