improve error handling

This commit is contained in:
1e99 2024-12-17 17:14:06 +01:00
parent 4ceec30e0c
commit 32dce28118
3 changed files with 24 additions and 12 deletions

View file

@ -1,5 +1,6 @@
{ {
"pixelchat.uploading": "Uploading", "pixelchat.uploading": "Uploading",
"pixelchat.upload_failed": "Upload failed", "pixelchat.upload_failed.no_error": "Upload failed",
"pixelchat.upload_failed.error": "Upload failed - %s",
"pixelchat.upload_completed": "Upload completed" "pixelchat.upload_completed": "Upload completed"
} }

View file

@ -46,21 +46,32 @@ public class ImageUploads {
this.minecraft.send(() -> bossBars.add(bossBar.getUuid(), bossBar)); this.minecraft.send(() -> bossBars.add(bossBar.getUuid(), bossBar));
String url = null; String url = null;
Throwable error = null;
for (String host : this.hosts) { for (String host : this.hosts) {
try { try {
url = this.upload(host, path); url = this.upload(host, path);
error = null;
break; break;
} catch (Throwable e) { } catch (Throwable e) {
error = e;
PixelChat.LOGGER.warn("Failed to upload", e); PixelChat.LOGGER.warn("Failed to upload", e);
} }
} }
// IntelliJ complains otherwise // IntelliJ complains otherwise
String finalUrl = url; String finalUrl = url;
Throwable finalError = error;
this.minecraft.send(() -> { this.minecraft.send(() -> {
if (finalUrl == null) { if (finalUrl == null) {
PixelChat.LOGGER.error("No uploader succeeded"); PixelChat.LOGGER.error("No uploader succeeded");
bossBar.setName(Text.translatable("pixelchat.upload_failed"));
Text message = finalError == null ?
Text.translatable("pixelchat.upload_failed.no_error") :
Text.translatable("pixelchat.upload_failed.error", finalError.getMessage());
bossBar.setName(message);
bossBar.setPercent(1.0f); bossBar.setPercent(1.0f);
bossBar.setColor(BossBar.Color.RED); bossBar.setColor(BossBar.Color.RED);
return; return;
@ -100,7 +111,7 @@ public class ImageUploads {
); );
if (res.statusCode() != 201) { if (res.statusCode() != 201) {
String body = res.body(); String body = res.body();
throw new RuntimeException(String.format("Failed to upload, expected status 201, got %d, body: %s", res.statusCode(), body)); throw new RuntimeException(String.format("%d: %s", res.statusCode(), body));
} }
String id = res.body(); String id = res.body();

View file

@ -6,10 +6,10 @@ import io.javalin.http.Context;
import io.javalin.http.HttpStatus; import io.javalin.http.HttpStatus;
import org.apache.commons.imaging.ImageFormats; import org.apache.commons.imaging.ImageFormats;
import org.apache.commons.imaging.Imaging; import org.apache.commons.imaging.Imaging;
import org.apache.commons.imaging.ImagingException;
import org.slf4j.Logger; import org.slf4j.Logger;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.time.Instant; import java.time.Instant;
import java.time.temporal.TemporalAmount; import java.time.temporal.TemporalAmount;
@ -51,22 +51,22 @@ public class ImageHandler {
status(HttpStatus.CREATED). status(HttpStatus.CREATED).
contentType(ContentType.TEXT_PLAIN). contentType(ContentType.TEXT_PLAIN).
result(id); result(id);
} catch (IllegalArgumentException ignored) { } catch (ImagingException | IllegalArgumentException ignored) {
ctx ctx
.status(HttpStatus.BAD_REQUEST) .status(HttpStatus.BAD_REQUEST)
.result("Unrecognized image format"); .result("Unrecognized image format");
} catch (IOException e) {
ctx
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.result("Internal Server Error");
this.logger.error("Failed to upload image", e);
} catch (OutOfMemoryError e) { } catch (OutOfMemoryError e) {
ctx ctx
.status(HttpStatus.INSUFFICIENT_STORAGE) .status(HttpStatus.INSUFFICIENT_STORAGE)
.result("Insufficient storage on server"); .result("Insufficient storage on server");
this.logger.error("Out of memory", e); this.logger.error("Out of memory", e);
} catch (Throwable e) {
ctx
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.result("Internal Server Error");
this.logger.error("Failed to upload image", e);
} }
} }
@ -86,7 +86,7 @@ public class ImageHandler {
status(HttpStatus.OK). status(HttpStatus.OK).
contentType(ContentType.IMAGE_PNG). contentType(ContentType.IMAGE_PNG).
result(png); result(png);
} catch (IOException e) { } catch (Throwable e) {
ctx ctx
.status(HttpStatus.INTERNAL_SERVER_ERROR) .status(HttpStatus.INTERNAL_SERVER_ERROR)
.result("Internal Server Error"); .result("Internal Server Error");