1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-12-24 00:38:52 -05:00

* added helper

This commit is contained in:
Reinhard Pointner 2014-07-28 12:54:27 +00:00
parent 90951f7989
commit 2ce7c6020b

View File

@ -1,63 +1,64 @@
package net.filebot.util;
import java.io.PrintWriter;
import java.io.StringWriter;
public final class ExceptionUtilities {
public static Throwable getRootCause(Throwable t) {
while (t.getCause() != null) {
t = t.getCause();
}
return t;
}
public static <T extends Throwable> T findCause(Throwable t, Class<T> type) {
while (t != null) {
if (type.isInstance(t))
return type.cast(t);
t = t.getCause();
}
return null;
}
public static String getRootCauseMessage(Throwable t) {
return getMessage(getRootCause(t));
}
public static String getMessage(Throwable t) {
String message = t.getMessage();
if (message == null || message.isEmpty()) {
message = t.toString();
}
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) {
if (type.isInstance(t)) {
return type.cast(t);
}
try {
return type.getConstructor(Throwable.class).newInstance(t);
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
}
public static RuntimeException asRuntimeException(Throwable t) {
return wrap(t, RuntimeException.class);
}
/**
* Dummy constructor to prevent instantiation.
@ -65,5 +66,5 @@ public final class ExceptionUtilities {
private ExceptionUtilities() {
throw new UnsupportedOperationException();
}
}