From 7abb96ead101b21d56556831e4f1d3cc231a5f6c Mon Sep 17 00:00:00 2001 From: Taylor Smock Date: Thu, 29 Dec 2022 07:28:08 -0700 Subject: [PATCH] MapillaryImageUtils: Handle cancellation or other errors better Signed-off-by: Taylor Smock --- .../mapillary/utils/MapillaryImageUtils.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/openstreetmap/josm/plugins/mapillary/utils/MapillaryImageUtils.java b/src/main/java/org/openstreetmap/josm/plugins/mapillary/utils/MapillaryImageUtils.java index 162830950..cd780ccd5 100644 --- a/src/main/java/org/openstreetmap/josm/plugins/mapillary/utils/MapillaryImageUtils.java +++ b/src/main/java/org/openstreetmap/josm/plugins/mapillary/utils/MapillaryImageUtils.java @@ -1,8 +1,10 @@ // License: GPL. For details, see LICENSE file. package org.openstreetmap.josm.plugins.mapillary.utils; +import java.io.IOException; import java.time.Instant; import java.util.Locale; +import java.util.concurrent.CancellationException; import java.util.concurrent.CompletableFuture; import java.util.function.Predicate; import java.util.regex.Matcher; @@ -13,6 +15,7 @@ import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry; import org.openstreetmap.josm.data.cache.CacheEntry; +import org.openstreetmap.josm.data.cache.ICachedLoaderListener; import org.openstreetmap.josm.data.osm.INode; import org.openstreetmap.josm.data.osm.IPrimitive; import org.openstreetmap.josm.data.osm.IWay; @@ -136,8 +139,17 @@ public static CompletableFuture getImage(@Nonnull INode } if (MapillaryImageUtils.isDownloadable(image)) { CompletableFuture completableFuture = new CompletableFuture<>(); - CacheUtils.submit(image, type, - (entry, attributes, result) -> cacheImageFuture(image, completableFuture, entry)); + CacheUtils.submit(image, type, (entry, attributes, result) -> { + if (result == ICachedLoaderListener.LoadResult.SUCCESS) { + cacheImageFuture(image, completableFuture, entry); + } else if (result == ICachedLoaderListener.LoadResult.CANCELED) { + completableFuture.completeExceptionally(new CancellationException( + "Mapillary Image download was cancelled: " + MapillaryImageUtils.getKey(image))); + } else { + completableFuture.completeExceptionally(new IOException( + "Mapillary Image could not be downloaded: " + MapillaryImageUtils.getKey(image))); + } + }); return completableFuture; } else if (getKey(image) > 0) { CompletableFuture completableFuture = new CompletableFuture<>();