From 2ce7c6020be4b04a8363234084f7c25f0fb19bc3 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Mon, 28 Jul 2014 12:54:27 +0000 Subject: [PATCH] * added helper --- .../net/filebot/util/ExceptionUtilities.java | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) 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(); } - + }