@Test public void testReversed() throws Exception { final Throwable ex = generateException(); final String rootCauseFirst = ExceptionUtil.rootCauseFirstStackTrace(ex); assertNotNull(rootCauseFirst, "Stacktrace was null"); assertTrue(rootCauseFirst.contains("Resulted in:"), "Stacktrace doesn't contain the \"Resulted in:\" string"); assertFalse(rootCauseFirst.contains("Caused by:"), "Stacktrace contains the \"Caused by:\" string"); final String[] lines = rootCauseFirst.split("\n"); final String firstLine = lines[0]; assertTrue(firstLine.startsWith(NumberFormatException.class.getName() + ": For input string: \"23.23232\""), "Unexpected root cause"); final List<String> expectedResultedIns = new ArrayList<>(); expectedResultedIns.add(IllegalArgumentException.class.getName() + ": Incorrect param"); expectedResultedIns.add(IOException.class.getName() + ": Request processing failed"); expectedResultedIns.add(IOError.class.getName()); expectedResultedIns.add(RuntimeException.class.getName() + ": Unexpected exception"); for (final String line : lines) { if (!line.startsWith("Resulted in:")) { continue; } final String expected = expectedResultedIns.remove(0); assertTrue(line.startsWith("Resulted in: " + expected), "Unexpected stacktrace element '" + line + "'"); } assertTrue(expectedResultedIns.isEmpty(), "Reversed stacktrace is missing certain elements"); }
public static String rootCauseFirstStackTrace(final Throwable exception) { if (exception == null) { return null; } final List<Throwable> exceptionChain = new ArrayList<>(); Throwable curr = exception; while (curr != null) { exceptionChain.add(0, curr); curr = curr.getCause(); } Throwable prevStrippedCause = null; Throwable modifiedRoot = null; for (int i = 0; i < exceptionChain.size(); i++) { final Throwable x = prevStrippedCause == null ? stripCause(exceptionChain.get(0)) : prevStrippedCause; if (i != exceptionChain.size() - 1) { final Throwable strippedCause = stripCause(exceptionChain.get(i + 1)); x.initCause(strippedCause); prevStrippedCause = strippedCause; } if (i == 0) { modifiedRoot = x; } } return generateStackTrace(modifiedRoot).replace("Caused by:", "Resulted in:"); }
ExceptionUtil { public static String rootCauseFirstStackTrace(final Throwable exception) { if (exception == null) { return null; } final List<Throwable> exceptionChain = new ArrayList<>(); Throwable curr = exception; while (curr != null) { exceptionChain.add(0, curr); curr = curr.getCause(); } Throwable prevStrippedCause = null; Throwable modifiedRoot = null; for (int i = 0; i < exceptionChain.size(); i++) { final Throwable x = prevStrippedCause == null ? stripCause(exceptionChain.get(0)) : prevStrippedCause; if (i != exceptionChain.size() - 1) { final Throwable strippedCause = stripCause(exceptionChain.get(i + 1)); x.initCause(strippedCause); prevStrippedCause = strippedCause; } if (i == 0) { modifiedRoot = x; } } return generateStackTrace(modifiedRoot).replace("Caused by:", "Resulted in:"); } }
ExceptionUtil { public static String rootCauseFirstStackTrace(final Throwable exception) { if (exception == null) { return null; } final List<Throwable> exceptionChain = new ArrayList<>(); Throwable curr = exception; while (curr != null) { exceptionChain.add(0, curr); curr = curr.getCause(); } Throwable prevStrippedCause = null; Throwable modifiedRoot = null; for (int i = 0; i < exceptionChain.size(); i++) { final Throwable x = prevStrippedCause == null ? stripCause(exceptionChain.get(0)) : prevStrippedCause; if (i != exceptionChain.size() - 1) { final Throwable strippedCause = stripCause(exceptionChain.get(i + 1)); x.initCause(strippedCause); prevStrippedCause = strippedCause; } if (i == 0) { modifiedRoot = x; } } return generateStackTrace(modifiedRoot).replace("Caused by:", "Resulted in:"); }  }
ExceptionUtil { public static String rootCauseFirstStackTrace(final Throwable exception) { if (exception == null) { return null; } final List<Throwable> exceptionChain = new ArrayList<>(); Throwable curr = exception; while (curr != null) { exceptionChain.add(0, curr); curr = curr.getCause(); } Throwable prevStrippedCause = null; Throwable modifiedRoot = null; for (int i = 0; i < exceptionChain.size(); i++) { final Throwable x = prevStrippedCause == null ? stripCause(exceptionChain.get(0)) : prevStrippedCause; if (i != exceptionChain.size() - 1) { final Throwable strippedCause = stripCause(exceptionChain.get(i + 1)); x.initCause(strippedCause); prevStrippedCause = strippedCause; } if (i == 0) { modifiedRoot = x; } } return generateStackTrace(modifiedRoot).replace("Caused by:", "Resulted in:"); }  static String generateStackTrace(final Throwable exception); static String rootCauseFirstStackTrace(final Throwable exception); }
ExceptionUtil { public static String rootCauseFirstStackTrace(final Throwable exception) { if (exception == null) { return null; } final List<Throwable> exceptionChain = new ArrayList<>(); Throwable curr = exception; while (curr != null) { exceptionChain.add(0, curr); curr = curr.getCause(); } Throwable prevStrippedCause = null; Throwable modifiedRoot = null; for (int i = 0; i < exceptionChain.size(); i++) { final Throwable x = prevStrippedCause == null ? stripCause(exceptionChain.get(0)) : prevStrippedCause; if (i != exceptionChain.size() - 1) { final Throwable strippedCause = stripCause(exceptionChain.get(i + 1)); x.initCause(strippedCause); prevStrippedCause = strippedCause; } if (i == 0) { modifiedRoot = x; } } return generateStackTrace(modifiedRoot).replace("Caused by:", "Resulted in:"); }  static String generateStackTrace(final Throwable exception); static String rootCauseFirstStackTrace(final Throwable exception);  }