* added helper
This commit is contained in:
parent
90951f7989
commit
2ce7c6020b
|
@ -1,63 +1,64 @@
|
||||||
|
|
||||||
package net.filebot.util;
|
package net.filebot.util;
|
||||||
|
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
import java.io.StringWriter;
|
||||||
|
|
||||||
public final class ExceptionUtilities {
|
public final class ExceptionUtilities {
|
||||||
|
|
||||||
public static Throwable getRootCause(Throwable t) {
|
public static Throwable getRootCause(Throwable t) {
|
||||||
while (t.getCause() != null) {
|
while (t.getCause() != null) {
|
||||||
t = t.getCause();
|
t = t.getCause();
|
||||||
}
|
}
|
||||||
|
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static <T extends Throwable> T findCause(Throwable t, Class<T> type) {
|
public static <T extends Throwable> T findCause(Throwable t, Class<T> type) {
|
||||||
while (t != null) {
|
while (t != null) {
|
||||||
if (type.isInstance(t))
|
if (type.isInstance(t))
|
||||||
return type.cast(t);
|
return type.cast(t);
|
||||||
|
|
||||||
t = t.getCause();
|
t = t.getCause();
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static String getRootCauseMessage(Throwable t) {
|
public static String getRootCauseMessage(Throwable t) {
|
||||||
return getMessage(getRootCause(t));
|
return getMessage(getRootCause(t));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static String getMessage(Throwable t) {
|
public static String getMessage(Throwable t) {
|
||||||
String message = t.getMessage();
|
String message = t.getMessage();
|
||||||
|
|
||||||
if (message == null || message.isEmpty()) {
|
if (message == null || message.isEmpty()) {
|
||||||
message = t.toString();
|
message = t.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String getStackTrace(Throwable t) {
|
||||||
|
StringWriter buffer = new StringWriter();
|
||||||
|
t.printStackTrace(new PrintWriter(buffer, true));
|
||||||
|
return buffer.getBuffer().toString();
|
||||||
|
}
|
||||||
|
|
||||||
public static <T extends Throwable> T wrap(Throwable t, Class<T> type) {
|
public static <T extends Throwable> T wrap(Throwable t, Class<T> type) {
|
||||||
if (type.isInstance(t)) {
|
if (type.isInstance(t)) {
|
||||||
return type.cast(t);
|
return type.cast(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return type.getConstructor(Throwable.class).newInstance(t);
|
return type.getConstructor(Throwable.class).newInstance(t);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new IllegalArgumentException(e);
|
throw new IllegalArgumentException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static RuntimeException asRuntimeException(Throwable t) {
|
public static RuntimeException asRuntimeException(Throwable t) {
|
||||||
return wrap(t, RuntimeException.class);
|
return wrap(t, RuntimeException.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dummy constructor to prevent instantiation.
|
* Dummy constructor to prevent instantiation.
|
||||||
|
@ -65,5 +66,5 @@ public final class ExceptionUtilities {
|
||||||
private ExceptionUtilities() {
|
private ExceptionUtilities() {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue