improve error handling
This commit is contained in:
parent
4ceec30e0c
commit
32dce28118
3 changed files with 24 additions and 12 deletions
|
@ -1,5 +1,6 @@
|
|||
{
|
||||
"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"
|
||||
}
|
|
@ -46,21 +46,32 @@ public class ImageUploads {
|
|||
this.minecraft.send(() -> bossBars.add(bossBar.getUuid(), bossBar));
|
||||
|
||||
String url = null;
|
||||
Throwable error = null;
|
||||
|
||||
for (String host : this.hosts) {
|
||||
try {
|
||||
url = this.upload(host, path);
|
||||
error = null;
|
||||
break;
|
||||
} catch (Throwable e) {
|
||||
error = e;
|
||||
PixelChat.LOGGER.warn("Failed to upload", e);
|
||||
}
|
||||
}
|
||||
|
||||
// IntelliJ complains otherwise
|
||||
String finalUrl = url;
|
||||
Throwable finalError = error;
|
||||
|
||||
this.minecraft.send(() -> {
|
||||
if (finalUrl == null) {
|
||||
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.setColor(BossBar.Color.RED);
|
||||
return;
|
||||
|
@ -100,7 +111,7 @@ public class ImageUploads {
|
|||
);
|
||||
if (res.statusCode() != 201) {
|
||||
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();
|
||||
|
|
|
@ -6,10 +6,10 @@ import io.javalin.http.Context;
|
|||
import io.javalin.http.HttpStatus;
|
||||
import org.apache.commons.imaging.ImageFormats;
|
||||
import org.apache.commons.imaging.Imaging;
|
||||
import org.apache.commons.imaging.ImagingException;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.TemporalAmount;
|
||||
|
@ -51,22 +51,22 @@ public class ImageHandler {
|
|||
status(HttpStatus.CREATED).
|
||||
contentType(ContentType.TEXT_PLAIN).
|
||||
result(id);
|
||||
} catch (IllegalArgumentException ignored) {
|
||||
} catch (ImagingException | IllegalArgumentException ignored) {
|
||||
ctx
|
||||
.status(HttpStatus.BAD_REQUEST)
|
||||
.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) {
|
||||
ctx
|
||||
.status(HttpStatus.INSUFFICIENT_STORAGE)
|
||||
.result("Insufficient storage on server");
|
||||
|
||||
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).
|
||||
contentType(ContentType.IMAGE_PNG).
|
||||
result(png);
|
||||
} catch (IOException e) {
|
||||
} catch (Throwable e) {
|
||||
ctx
|
||||
.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
.result("Internal Server Error");
|
||||
|
|
Loading…
Reference in a new issue