From c9c2ebc5325e3c11fa9f6ca2193fad5a7095f3cc Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Thu, 23 Jul 2015 22:50:20 +1000 Subject: [PATCH] 473266 - Better handling of MultiException Add extra exceptions as suppressed exceptions --- .../eclipse/jetty/util/MultiException.java | 52 ++----------------- .../org/eclipse/jetty/util/log/StdErrLog.java | 21 ++++++-- .../eclipse/jetty/util/log/StdErrLogTest.java | 20 +++++++ 3 files changed, 40 insertions(+), 53 deletions(-) diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/MultiException.java b/jetty-util/src/main/java/org/eclipse/jetty/util/MultiException.java index 5dd913feb27..f3e7d39d212 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/MultiException.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/MultiException.java @@ -18,8 +18,6 @@ package org.eclipse.jetty.util; -import java.io.PrintStream; -import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -45,12 +43,14 @@ public class MultiException extends Exception { if (e==null) throw new IllegalArgumentException(); - + if(nested == null) { initCause(e); nested = new ArrayList<>(); } + else + addSuppressed(e); if (e instanceof MultiException) { @@ -70,9 +70,8 @@ public class MultiException extends Exception /* ------------------------------------------------------------ */ public List getThrowables() { - if(nested == null) { + if(nested == null) return Collections.emptyList(); - } return nested; } @@ -172,47 +171,4 @@ public class MultiException extends Exception return str.toString(); } - /* ------------------------------------------------------------ */ - @Override - public void printStackTrace() - { - super.printStackTrace(); - if(nested != null) { - for(Throwable t: nested) { - t.printStackTrace(); - } - } - } - - - /* ------------------------------------------------------------------------------- */ - /** - * @see java.lang.Throwable#printStackTrace(java.io.PrintStream) - */ - @Override - public void printStackTrace(PrintStream out) - { - super.printStackTrace(out); - if(nested != null) { - for(Throwable t: nested) { - t.printStackTrace(out); - } - } - } - - /* ------------------------------------------------------------------------------- */ - /** - * @see java.lang.Throwable#printStackTrace(java.io.PrintWriter) - */ - @Override - public void printStackTrace(PrintWriter out) - { - super.printStackTrace(out); - if(nested != null) { - for(Throwable t: nested) { - t.printStackTrace(out); - } - } - } - } diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/log/StdErrLog.java b/jetty-util/src/main/java/org/eclipse/jetty/util/log/StdErrLog.java index 66e6371c0ad..dfc91684755 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/log/StdErrLog.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/log/StdErrLog.java @@ -694,7 +694,12 @@ public class StdErrLog extends AbstractLogger builder.append(string); } - private void format(StringBuilder buffer, Throwable thrown) + protected void format(StringBuilder buffer, Throwable thrown) + { + format(buffer,thrown,""); + } + + protected void format(StringBuilder buffer, Throwable thrown, String indent) { if (thrown == null) { @@ -702,20 +707,26 @@ public class StdErrLog extends AbstractLogger } else { - buffer.append(EOL); + buffer.append(EOL).append(indent); format(buffer,thrown.toString()); StackTraceElement[] elements = thrown.getStackTrace(); for (int i = 0; elements != null && i < elements.length; i++) { - buffer.append(EOL).append("\tat "); + buffer.append(EOL).append(indent).append("\tat "); format(buffer,elements[i].toString()); } + for (Throwable suppressed:thrown.getSuppressed()) + { + buffer.append(EOL).append(indent).append("Suppressed: "); + format(buffer,suppressed,"\t|"+indent); + } + Throwable cause = thrown.getCause(); if (cause != null && cause != thrown) { - buffer.append(EOL).append("Caused by: "); - format(buffer,cause); + buffer.append(EOL).append(indent).append("Caused by: "); + format(buffer,cause,indent); } } } diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/log/StdErrLogTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/log/StdErrLogTest.java index 73633da1b84..38fd37e517f 100644 --- a/jetty-util/src/test/java/org/eclipse/jetty/util/log/StdErrLogTest.java +++ b/jetty-util/src/test/java/org/eclipse/jetty/util/log/StdErrLogTest.java @@ -22,6 +22,7 @@ import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.is; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.PrintStream; import java.io.PrintWriter; import java.io.StringWriter; @@ -683,6 +684,25 @@ public class StdErrLogTest assertLevel(log,StdErrLog.LEVEL_WARN); // as configured } + @Test + public void testSuppressed() + { + StdErrLog log = new StdErrLog("xxx",new Properties()); + StdErrCapture output = new StdErrCapture(log); + + Exception inner = new Exception("inner"); + inner.addSuppressed( new IllegalStateException(){{addSuppressed(new Exception("branch0"));}}); + IOException outer = new IOException("outer",inner); + + outer.addSuppressed( new IllegalStateException(){{addSuppressed(new Exception("branch1"));}}); + outer.addSuppressed( new IllegalArgumentException(){{addSuppressed(new Exception("branch2"));}}); + + log.warn("problem",outer); + + output.assertContains("\t|\t|java.lang.Exception: branch2"); + output.assertContains("\t|\t|java.lang.Exception: branch1"); + output.assertContains("\t|\t|java.lang.Exception: branch0"); + } private void assertLevel(StdErrLog log, int expectedLevel) {