/** * return the top n lines of this stack trace. * * <p>Example: ExceptionUtil.getStackTraceLines(exception, 10);</p> */ public static String getStackTraceLines(Throwable t, int numLines) { StringTokenizer tokens = new StringTokenizer(toString(t), "\n\r"); StringBuffer trace = new StringBuffer(); for (int i=0; i<numLines; i++) { String token = tokens.nextToken(); trace.append(token); trace.append(StringUtils.NEWLINE); } return trace.toString(); }
/** * Get to the base exception (if any). * @param t the Throwable object * @return the base Exception object */ public static Throwable baseException(Throwable t) { Throwable wrapped = wrappedException(t); return wrapped != null? baseException(wrapped) : t; }
/** * Write the string as a line to the trace stream. If the * "stack" property is enabled, then the caller's stack call * is also shown in the date. * * @param str string to write to the trace stream */ public static void trace(String str) { if (timeStamp) System.err.print(new Date()); if(caller) System.err.print(ExceptionUtil.getCallAt(new Throwable(), 1) + " "); System.err.println(str); if (stackLines > 0) System.err.println(ExceptionUtil.getStackTraceLines(new Throwable(), stackLines)); else if (stackPrefix != null) System.err.println(ExceptionUtil.getStackTraceUpTo(new Throwable(), stackPrefix)); } }
/** * Utility method for converting an exception and all chained root causes into a * string. Unlike <code>exceptionToString(Throwable)</code> which prints the chain * from most nested root cause down to the top-level exception, this method prints * from the top-level exception down to the most nested root cause. * * @param t The throwable exception. * @return The printable exception. */ public static String exceptionFollowedByRootCausesToString(Throwable t) { StringBuffer output = new StringBuffer(); Throwable root = t; while (root != null) { output.append((root == t) ? ((root instanceof Exception) ? " Exception: " : " Error: ") : " Root cause: "); output.append(ExceptionUtil.toString(root)); // Do not recurse if the root cause has already been printed; this will have happened if the root cause has // been assigned to the current Throwable via initCause() or as a constructor argument. Throwable cause = root.getCause(); root = ExceptionUtil.wrappedException(root); if (cause == root) break; } return output.toString(); }
Log.getLogger(LogCategories.CONFIGURATION).error("MessageBroker failed to start: " + ExceptionUtil.exceptionFollowedByRootCausesToString(e));
public static Throwable wrappedException(Throwable t) { // handle these statically since they are core to Java if (t instanceof InvocationTargetException) { return ((InvocationTargetException)t).getTargetException(); } return getRootCauseWithReflection(t); }
/** * Utility method for converting an exception and all chained root causes into a * string. Unlike <code>exceptionToString(Throwable)</code> which prints the chain * from most nested root cause down to the top-level exception, this method prints * from the top-level exception down to the most nested root cause. * * @param t The throwable exception. * @return The printable exception. */ public static String exceptionFollowedByRootCausesToString(Throwable t) { StringBuffer output = new StringBuffer(); Throwable root = t; while (root != null) { output.append((root == t) ? ((root instanceof Exception) ? " Exception: " : " Error: ") : " Root cause: "); output.append(ExceptionUtil.toString(root)); // Do not recurse if the root cause has already been printed; this will have happened if the root cause has // been assigned to the current Throwable via initCause() or as a constructor argument. Throwable cause = root.getCause(); root = ExceptionUtil.wrappedException(root); if (cause == root) break; } return output.toString(); }
output.append(ExceptionUtil.exceptionFollowedByRootCausesToString(this)); output.append(StringUtils.NEWLINE);
/** * Get the wrapped Exception object from the Throwable object. * @param t the Throwable object * @return Throwable the wrapped exception object if any */ public static Throwable wrappedException(Throwable t) { // Handle these statically since they are core to Java return (t instanceof InvocationTargetException)? ((InvocationTargetException)t).getTargetException() : getRootCauseWithReflection(t); }
/** * return the top n lines of this stack trace. * * <p>Example: ExceptionUtil.getStackTraceLines(exception, 10);</p> * @param t the Throwable object * @param numLines number of lines we should trace down * @return String the String of stack trace lines */ public static String getStackTraceLines(Throwable t, int numLines) { StringTokenizer tokens = new StringTokenizer(toString(t), "\n\r"); StringBuffer trace = new StringBuffer(); for (int i=0; i<numLines; i++) { String token = tokens.nextToken(); trace.append(token); trace.append(StringUtils.NEWLINE); } return trace.toString(); }
/** * Utility method for converting an exception and all chained root causes into a * string. Unlike <code>exceptionToString(Throwable)</code> which prints the chain * from most nested root cause down to the top-level exception, this method prints * from the top-level exception down to the most nested root cause. * * @param t The throwable exception. * @return The printable exception. */ public static String exceptionFollowedByRootCausesToString(Throwable t) { StringBuffer output = new StringBuffer(); Throwable root = t; while (root != null) { output.append((root == t) ? ((root instanceof Exception) ? " Exception: " : " Error: ") : " Root cause: "); output.append(ExceptionUtil.toString(root)); // Do not recurse if the root cause has already been printed; this will have happened if the root cause has // been assigned to the current Throwable via initCause() or as a constructor argument. Throwable cause = root.getCause(); root = ExceptionUtil.wrappedException(root); if (cause == root) break; } return output.toString(); }
/** * Write the string as a line to the trace stream. If the * "stack" property is enabled, then the caller's stack call * is also shown in the date. * * @param str string to write to the trace stream */ public static void trace(String str) { if (timeStamp) System.err.print(new Date()); if(caller) System.err.print(ExceptionUtil.getCallAt(new Throwable(), 1) + " "); System.err.println(str); if (stackLines > 0) System.err.println(ExceptionUtil.getStackTraceLines(new Throwable(), stackLines)); else if (stackPrefix != null) System.err.println(ExceptionUtil.getStackTraceUpTo(new Throwable(), stackPrefix)); } }
output.append(ExceptionUtil.exceptionFollowedByRootCausesToString(this)); output.append(StringUtils.NEWLINE);
/** * Get to the base exception (if any). */ public static Throwable baseException(Throwable t) { Throwable wrapped = wrappedException(t); if (wrapped != null) return baseException(wrapped); else return t; }
/** * Get the wrapped Exception object from the Throwable object. * @param t the Throwable object * @return Throwable the wrapped exception object if any */ public static Throwable wrappedException(Throwable t) { // Handle these statically since they are core to Java return (t instanceof InvocationTargetException)? ((InvocationTargetException)t).getTargetException() : getRootCauseWithReflection(t); }
/** * return the top n lines of this stack trace. * * <p>Example: ExceptionUtil.getStackTraceLines(exception, 10);</p> * @param t the Throwable object * @param numLines number of lines we should trace down * @return String the String of stack trace lines */ public static String getStackTraceLines(Throwable t, int numLines) { StringTokenizer tokens = new StringTokenizer(toString(t), "\n\r"); StringBuffer trace = new StringBuffer(); for (int i=0; i<numLines; i++) { String token = tokens.nextToken(); trace.append(token); trace.append(StringUtils.NEWLINE); } return trace.toString(); }
/** * Write the string as a line to the trace stream. If the * "stack" property is enabled, then the caller's stack call * is also shown in the date. * * @param str string to write to the trace stream */ public static void trace(String str) { if (timeStamp) System.err.print(new Date()); if(caller) System.err.print(ExceptionUtil.getCallAt(new Throwable(), 1) + " "); System.err.println(str); if (stackLines > 0) System.err.println(ExceptionUtil.getStackTraceLines(new Throwable(), stackLines)); else if (stackPrefix != null) System.err.println(ExceptionUtil.getStackTraceUpTo(new Throwable(), stackPrefix)); } }
output.append(ExceptionUtil.exceptionFollowedByRootCausesToString(this)); output.append(StringUtils.NEWLINE);
/** * Get to the base exception (if any). * @param t the Throwable object * @return the base Exception object */ public static Throwable baseException(Throwable t) { Throwable wrapped = wrappedException(t); return wrapped != null? baseException(wrapped) : t; }
/** * Return the stack trace up to the first line that starts with prefix. * * <p>Example: ExceptionUtil.getStackTraceUpTo(exception, "jrunx.");</p> */ public static String getStackTraceUpTo(Throwable t, String prefix) { StringTokenizer tokens = new StringTokenizer(toString(t), "\n\r"); StringBuffer trace = new StringBuffer(); boolean done = false; String lookingFor = "at " + prefix; while (!done && tokens.hasMoreElements()) { String token = tokens.nextToken(); if (token.indexOf(lookingFor) == -1) trace.append(token); else done = true; trace.append(StringUtils.NEWLINE); } return trace.toString(); }