diff --git a/source/net/filebot/util/ExceptionUtilities.java b/source/net/filebot/util/ExceptionUtilities.java index f35b1a73..d8ac8e41 100644 --- a/source/net/filebot/util/ExceptionUtilities.java +++ b/source/net/filebot/util/ExceptionUtilities.java @@ -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 findCause(Throwable t, Class 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 wrap(Throwable t, Class 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(); } - + }