Small fixes for error handling

This commit is contained in:
Christopher Schnick 2022-12-31 22:13:07 +01:00
parent 423fad1f05
commit 742dde4820
5 changed files with 43 additions and 22 deletions

View file

@ -0,0 +1,7 @@
package io.xpipe.core.util;
import lombok.experimental.StandardException;
@StandardException
public class ValidationException extends Exception {
}

View file

@ -27,4 +27,6 @@ public interface I18n {
String getKey(String s);
String getLocalised(String s, Object... vars);
boolean isLoaded();
}

View file

@ -36,6 +36,7 @@ public class ErrorEvent {
}
public void handle() {
EventHandler.get().modify(this);
EventHandler.get().handle(this);
}
@ -50,6 +51,10 @@ public class ErrorEvent {
public static class ErrorEventBuilder {
public ErrorEventBuilder term() {
return terminal(true);
}
public ErrorEventBuilder omit() {
return omitted(true);
}

View file

@ -30,6 +30,11 @@ public abstract class EventHandler {
ee.getThrowable().printStackTrace();
}
}
@Override
public void modify(ErrorEvent ee) {
}
};
public static final EventHandler OMIT = new EventHandler() {
@ -45,6 +50,11 @@ public abstract class EventHandler {
@Override
public void handle(ErrorEvent ee) {
}
@Override
public void modify(ErrorEvent ee) {
}
};
private static EventHandler INSTANCE;
@ -68,4 +78,6 @@ public abstract class EventHandler {
public abstract void handle(TrackEvent te);
public abstract void handle(ErrorEvent ee);
public abstract void modify(ErrorEvent ee);
}

View file

@ -8,30 +8,25 @@ public class ExceptionConverter {
public static String convertMessage(Throwable ex) {
var msg = ex.getLocalizedMessage();
if (ex instanceof StackOverflowError) {
return I18n.get("extension.stackOverflow", msg);
if (!I18n.INSTANCE.isLoaded()) {
return msg;
}
if (ex instanceof FileNotFoundException) {
return I18n.get("extension.fileNotFound", msg);
}
return switch (ex) {
case StackOverflowError e -> I18n.get("extension.stackOverflow");
case OutOfMemoryError e -> I18n.get("extension.outOfMemory");
case FileNotFoundException e -> I18n.get("extension.fileNotFound", msg);
case NullPointerException e -> I18n.get("extension.nullPointer");
case UnsupportedOperationException e -> I18n.get("extension.unsupportedOperation", msg);
case ClassNotFoundException e -> I18n.get("extension.classNotFound", msg);
default -> {
if (msg == null || msg.trim().length() == 0) {
yield I18n.get("extension.noInformationAvailable");
}
if (ex instanceof UnsupportedOperationException) {
return I18n.get("extension.unsupportedOperation", msg);
}
if (ex instanceof ClassNotFoundException) {
return I18n.get("extension.classNotFound", msg);
}
if (ex instanceof NullPointerException) {
return I18n.get("extension.nullPointer", msg);
}
if (msg == null || msg.trim().length() == 0) {
return I18n.get("extension.noInformationAvailable");
}
return msg;
yield msg;
}
};
}
}