473266 - Better handling of MultiException
Add extra exceptions as suppressed exceptions
This commit is contained in:
parent
ac8316756c
commit
c9c2ebc532
|
@ -18,8 +18,6 @@
|
||||||
|
|
||||||
package org.eclipse.jetty.util;
|
package org.eclipse.jetty.util;
|
||||||
|
|
||||||
import java.io.PrintStream;
|
|
||||||
import java.io.PrintWriter;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -45,12 +43,14 @@ public class MultiException extends Exception
|
||||||
{
|
{
|
||||||
if (e==null)
|
if (e==null)
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
|
|
||||||
if(nested == null)
|
if(nested == null)
|
||||||
{
|
{
|
||||||
initCause(e);
|
initCause(e);
|
||||||
nested = new ArrayList<>();
|
nested = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
addSuppressed(e);
|
||||||
|
|
||||||
if (e instanceof MultiException)
|
if (e instanceof MultiException)
|
||||||
{
|
{
|
||||||
|
@ -70,9 +70,8 @@ public class MultiException extends Exception
|
||||||
/* ------------------------------------------------------------ */
|
/* ------------------------------------------------------------ */
|
||||||
public List<Throwable> getThrowables()
|
public List<Throwable> getThrowables()
|
||||||
{
|
{
|
||||||
if(nested == null) {
|
if(nested == null)
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
|
||||||
return nested;
|
return nested;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -172,47 +171,4 @@ public class MultiException extends Exception
|
||||||
return str.toString();
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -694,7 +694,12 @@ public class StdErrLog extends AbstractLogger
|
||||||
builder.append(string);
|
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)
|
if (thrown == null)
|
||||||
{
|
{
|
||||||
|
@ -702,20 +707,26 @@ public class StdErrLog extends AbstractLogger
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
buffer.append(EOL);
|
buffer.append(EOL).append(indent);
|
||||||
format(buffer,thrown.toString());
|
format(buffer,thrown.toString());
|
||||||
StackTraceElement[] elements = thrown.getStackTrace();
|
StackTraceElement[] elements = thrown.getStackTrace();
|
||||||
for (int i = 0; elements != null && i < elements.length; i++)
|
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());
|
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();
|
Throwable cause = thrown.getCause();
|
||||||
if (cause != null && cause != thrown)
|
if (cause != null && cause != thrown)
|
||||||
{
|
{
|
||||||
buffer.append(EOL).append("Caused by: ");
|
buffer.append(EOL).append(indent).append("Caused by: ");
|
||||||
format(buffer,cause);
|
format(buffer,cause,indent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,7 @@ import static org.hamcrest.Matchers.containsString;
|
||||||
import static org.hamcrest.Matchers.is;
|
import static org.hamcrest.Matchers.is;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
|
@ -683,6 +684,25 @@ public class StdErrLogTest
|
||||||
assertLevel(log,StdErrLog.LEVEL_WARN); // as configured
|
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)
|
private void assertLevel(StdErrLog log, int expectedLevel)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue