Skip to content

Commit

Permalink
Fixes silent error when entering an invalid URL in an image input
Browse files Browse the repository at this point in the history
  • Loading branch information
Zailer43 committed Sep 10, 2024
1 parent dc8659c commit 176f77d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,17 @@ public void loadImageFromText(IImageLoaderFromText imageLoaderFromText, String v
.build();

CompletableFuture.supplyAsync(() -> {
MinecraftClient.getInstance().execute(() -> {
this.active = true;
SnackBarManager.getInstance().add(loadingSnackBar);
});
MinecraftClient.getInstance().execute(() -> SnackBarManager.getInstance().add(loadingSnackBar));

return imageLoaderFromText.loadImage(value);
}).thenApply(status -> {
}).whenComplete((status, throwable) -> {
Optional<BufferedImage> image = imageLoaderFromText.getImage();

if (throwable != null) {
FzmmClient.LOGGER.error("[ImageButtonComponent] Unexpected error loading an image", throwable);
status = ImageStatus.UNEXPECTED_ERROR;
}

SnackBarBuilder snackBarStatus = BaseSnackBarComponent.builder(SnackBarManager.IMAGE_ID)
.title(status.getStatusTranslation())
.backgroundColor(status.getColor());
Expand All @@ -90,7 +92,7 @@ public void loadImageFromText(IImageLoaderFromText imageLoaderFromText, String v
} else {
if (this.imageLoadEvent != null) {
assert image.isPresent();
status = this.imageLoadEvent.apply(image.get());
this.imageLoadEvent.apply(image.get());
}
snackBarStatus.lowTimer();
FzmmClient.LOGGER.info("[ImageButtonComponent] Image loaded successfully");
Expand All @@ -102,14 +104,12 @@ public void loadImageFromText(IImageLoaderFromText imageLoaderFromText, String v
SnackBarManager.getInstance().add(snackBarStatus.startTimer().build());
});


if (this.image != null) {
this.image.flush();
}

this.image = image.orElse(null);
return status;
}).thenAccept(status -> {

if (this.callback != null) {
this.callback.accept(this.image);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,18 @@ public ImageStatus loadImage(String value) {
}
this.image = null;
try {
if (value.isEmpty())
if (value.isEmpty()) {
return ImageStatus.NO_IMAGE_LOADED;
}

Optional<BufferedImage> optionalImage = ImageUtils.getImageFromUrl(value);
this.image = optionalImage.orElse(null);

return optionalImage.isEmpty() ? ImageStatus.URL_HAS_NO_IMAGE : ImageStatus.IMAGE_LOADED;
} catch (MalformedURLException ignored) {
} catch (MalformedURLException | IllegalArgumentException ignored) {
return ImageStatus.MALFORMED_URL;
} catch (IOException e) {
FzmmClient.LOGGER.error("Unexpected error loading an image", e);
FzmmClient.LOGGER.error("[ImageUrlSource] Unexpected error loading an image", e);
return ImageStatus.UNEXPECTED_ERROR;
}
}
Expand Down

0 comments on commit 176f77d

Please sign in to comment.