* added helper

This commit is contained in:
Reinhard Pointner 2014-07-28 12:54:27 +00:00
parent 90951f7989
commit 2ce7c6020b
1 changed files with 16 additions and 15 deletions

View File

@ -1,6 +1,7 @@
package net.filebot.util; package net.filebot.util;
import java.io.PrintWriter;
import java.io.StringWriter;
public final class ExceptionUtilities { public final class ExceptionUtilities {
@ -12,7 +13,6 @@ public final class ExceptionUtilities {
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))
@ -24,12 +24,10 @@ public final class ExceptionUtilities {
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();
@ -40,6 +38,11 @@ public final class ExceptionUtilities {
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)) {
@ -53,12 +56,10 @@ public final class ExceptionUtilities {
} }
} }
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.
*/ */