@Override public void onError(Throwable t) { Throwable e = error; if (e == null) { actual.onError(t); } else { actual.onError(new CompositeException(e, t)); } }
/** * Cast the given Throwable to CompositeException and returns its inner * Throwable list. * @param ex the target Throwable * @return the list of Throwables */ public static List<Throwable> compositeList(Throwable ex) { if (ex instanceof UndeliverableException) { ex = ex.getCause(); } return ((CompositeException)ex).getExceptions(); }
@Test public void testNullCollection() { CompositeException composite = new CompositeException((List<Throwable>)null); composite.getCause(); composite.printStackTrace(); } @Test
/** * Special handling for printing out a {@code CompositeException}. * Loops through all inner exceptions and prints them out. * * @param s * stream to print to */ private void printStackTrace(PrintStreamOrWriter s) { StringBuilder b = new StringBuilder(128); b.append(this).append('\n'); for (StackTraceElement myStackElement : getStackTrace()) { b.append("\tat ").append(myStackElement).append('\n'); } int i = 1; for (Throwable ex : exceptions) { b.append(" ComposedException ").append(i).append(" :\n"); appendStackTrace(b, ex, "\t"); i++; } s.println(b.toString()); }
List<Throwable> listOfCauses = getListOfCauses(e); chain = getRootCause(chain);
@Test(timeout = 1000) public void testCompositeExceptionFromChildThenParent() { CompositeException cex = new CompositeException(ex2, ex1); System.err.println("----------------------------- print composite stacktrace"); cex.printStackTrace(); assertEquals(2, cex.getExceptions().size()); assertNoCircularReferences(cex); assertNotNull(getRootCause(cex)); System.err.println("----------------------------- print cause stacktrace"); cex.getCause().printStackTrace(); }
@Test public void constructorWithNull() { assertTrue(new CompositeException((Throwable[])null).getExceptions().get(0) instanceof NullPointerException); assertTrue(new CompositeException((Iterable<Throwable>)null).getExceptions().get(0) instanceof NullPointerException); assertTrue(new CompositeException(null, new TestException()).getExceptions().get(0) instanceof NullPointerException); }
@Test public void printStackTrace() { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); new CompositeException(new TestException()).printStackTrace(pw); assertTrue(sw.toString().contains("TestException")); }
/** * All of the following {@code printStackTrace} functionality is derived from JDK {@link Throwable} * {@code printStackTrace}. In particular, the {@code PrintStreamOrWriter} abstraction is copied wholesale. * * Changes from the official JDK implementation:<ul> * <li>no infinite loop detection</li> * <li>smaller critical section holding {@link PrintStream} lock</li> * <li>explicit knowledge about the exceptions {@link List} that this loops through</li> * </ul> */ @Override public void printStackTrace() { printStackTrace(System.err); }
@Test public void messageCollection() { CompositeException compositeException = new CompositeException(ex1, ex3); assertEquals("2 exceptions occurred. ", compositeException.getMessage()); }
@Test public void nullRootCause() { RuntimeException te = new RuntimeException() { private static final long serialVersionUID = -8492568224555229753L; @Override public Throwable getCause() { return null; } }; assertSame(te, new CompositeException(te).getCause().getCause()); assertSame(te, new CompositeException(new RuntimeException(te)).getCause().getCause().getCause()); }
@Test public void onErrorResumeNextFunctionReturnsNull() { try { error.onErrorResumeNext(new Function<Throwable, Single<Integer>>() { @Override public Single<Integer> apply(Throwable e) { return null; } }).blockingGet(); } catch (CompositeException ex) { assertTrue(ex.toString(), ex.getExceptions().get(1) instanceof NullPointerException); } }
@Test public void testMultipleErrors() { TestSubscriber<Integer> ts = new TestSubscriber<Integer>(); ts.onSubscribe(EmptySubscription.INSTANCE); ts.onError(new TestException()); ts.onError(new TestException()); try { ts.assertNoErrors(); } catch (AssertionError ex) { Throwable e = ex.getCause(); if (!(e instanceof CompositeException)) { fail("Multiple Error present but the reported error doesn't have a composite cause!"); } CompositeException ce = (CompositeException)e; if (ce.size() != 2) { ce.printStackTrace(); } assertEquals(2, ce.size()); // expected return; } fail("Multiple Error present but no assertion error!"); }
private void appendStackTrace(StringBuilder b, Throwable ex, String prefix) { b.append(prefix).append(ex).append('\n'); for (StackTraceElement stackElement : ex.getStackTrace()) { b.append("\t\tat ").append(stackElement).append('\n'); } if (ex.getCause() != null) { b.append("\tCaused by: "); appendStackTrace(b, ex.getCause(), ""); } }
@Test(timeout = 1000) public void testCompositeExceptionFromParentThenChild() { CompositeException cex = new CompositeException(ex1, ex2); System.err.println("----------------------------- print composite stacktrace"); cex.printStackTrace(); assertEquals(2, cex.getExceptions().size()); assertNoCircularReferences(cex); assertNotNull(getRootCause(cex)); System.err.println("----------------------------- print cause stacktrace"); cex.getCause().printStackTrace(); }
/** * All of the following {@code printStackTrace} functionality is derived from JDK {@link Throwable} * {@code printStackTrace}. In particular, the {@code PrintStreamOrWriter} abstraction is copied wholesale. * * Changes from the official JDK implementation:<ul> * <li>no infinite loop detection</li> * <li>smaller critical section holding {@link PrintStream} lock</li> * <li>explicit knowledge about the exceptions {@link List} that this loops through</li> * </ul> */ @Override public void printStackTrace() { printStackTrace(System.err); }
@Test public void messageVarargs() { CompositeException compositeException = new CompositeException(ex1, ex2, ex3); assertEquals("3 exceptions occurred. ", compositeException.getMessage()); }
@Test public void badException() { Throwable e = new BadException(); assertSame(e, new CompositeException(e).getCause().getCause()); assertSame(e, new CompositeException(new RuntimeException(e)).getCause().getCause().getCause()); } }
/** * Special handling for printing out a {@code CompositeException}. * Loops through all inner exceptions and prints them out. * * @param s * stream to print to */ private void printStackTrace(PrintStreamOrWriter s) { StringBuilder b = new StringBuilder(128); b.append(this).append('\n'); for (StackTraceElement myStackElement : getStackTrace()) { b.append("\tat ").append(myStackElement).append('\n'); } int i = 1; for (Throwable ex : exceptions) { b.append(" ComposedException ").append(i).append(" :\n"); appendStackTrace(b, ex, "\t"); i++; } s.println(b.toString()); }
List<Throwable> listOfCauses = getListOfCauses(e); chain = getRootCause(chain);