private ShouldHaveSuppressedException(Throwable actual, Throwable expectedSuppressedException) { super("%n" + "Expecting:%n" + " <%s>%n" + "to have a suppressed exception with the following type and message:%n" + " <%s> / <%s>%n" + "but could not find any in actual's suppressed exceptions:%n" + " <%s>.", actual, expectedSuppressedException.getClass().getName(), expectedSuppressedException.getMessage(), actual.getSuppressed()); }
private ShouldHaveSuppressedException(Throwable actual, Throwable expectedSuppressedException) { super("%n" + "Expecting:%n" + " <%s>%n" + "to have a suppressed exception with the following type and message:%n" + " <%s> / <%s>%n" + "but could not find any in actual's suppressed exceptions:%n" + " <%s>.", actual, expectedSuppressedException.getClass().getName(), expectedSuppressedException.getMessage(), actual.getSuppressed()); }
/** {@inheritDoc} */ @Override public void serialize(Throwable e, JsonGenerator gen, SerializerProvider ser) throws IOException { gen.writeStartObject(); writeException(e, gen); if (e.getCause() != null) gen.writeObjectField("cause", e.getCause()); if (!F.isEmpty(e.getSuppressed())) { gen.writeArrayFieldStart("suppressed"); for (Throwable sup : e.getSuppressed()) gen.writeObject(sup); gen.writeEndArray(); } gen.writeEndObject(); } };
@VisibleForTesting static RuntimeException convertToSerializableSparkException(Throwable error) { RuntimeException serializableThrowable = new RuntimeException( error.getClass().getName() + ": " + Objects.toString(error.getMessage(), ""), error.getCause() == null ? null : convertToSerializableSparkException( error.getCause())); serializableThrowable.setStackTrace(error.getStackTrace()); Arrays.stream(error.getSuppressed()) .map(JobResultSerializer::convertToSerializableSparkException) .forEach(serializableThrowable::addSuppressed); return serializableThrowable; } }
@Override protected boolean matchesSafely( Throwable item ) { return Arrays.equals( item.getSuppressed(), expectedSuppressedErrors ); }
/** * Collects suppressed exceptions from throwable and all it causes. * * @param t Throwable. * @return List of suppressed throwables. */ public static List<Throwable> getSuppressedList(@Nullable Throwable t) { List<Throwable> result = new ArrayList<>(); if (t == null) return result; do { for (Throwable suppressed : t.getSuppressed()) { result.add(suppressed); result.addAll(getSuppressedList(suppressed)); } } while ((t = t.getCause()) != null); return result; }
/** * Checks if passed throwable has given class in one of the suppressed exceptions. * * @param t Throwable to check (if {@code null}, {@code false} is returned). * @param cls Class to check. * @return {@code True} if one of the suppressed exceptions is an instance of passed class, * {@code false} otherwise. */ public static boolean hasSuppressed(@Nullable Throwable t, @Nullable Class<? extends Throwable> cls) { if (t == null || cls == null) return false; for (Throwable th : t.getSuppressed()) { if (cls.isAssignableFrom(th.getClass())) return true; if (hasSuppressed(th, cls)) return true; } return false; }
doPrintStackTrace(s, lower, t, prefix, encountered); for (Throwable suppressed : t.getSuppressed()) { s.append(prefix).append("Also: "); doPrintStackTrace(s, suppressed, t, prefix + "\t", encountered);
public void assertHasNoSuppressedExceptions(AssertionInfo info, Throwable actual) { assertNotNull(info, actual); Throwable[] suppressed = actual.getSuppressed(); if (suppressed.length != 0) throw failures.failure(info, shouldHaveNoSuppressedExceptions(suppressed)); }
@Override public void writeObject(FSTObjectOutput out, Object toWrite, FSTClazzInfo clzInfo, FSTClazzInfo.FSTFieldInfo referencedBy, int streamPosition) throws IOException { Throwable t = (Throwable)toWrite; out.writeStringUTF(t.getMessage()); StackTraceElement[] ste = t.getStackTrace(); out.writeObject(ste); out.writeObject(t.getCause()); out.writeObject(t.getSuppressed()); }
public void assertHasNoSuppressedExceptions(AssertionInfo info, Throwable actual) { assertNotNull(info, actual); Throwable[] suppressed = actual.getSuppressed(); if (suppressed.length != 0) throw failures.failure(info, shouldHaveNoSuppressedExceptions(suppressed)); }
public void assertHasSuppressedException(AssertionInfo info, Throwable actual, Throwable expectedSuppressedException) { assertNotNull(info, actual); checkNotNull(expectedSuppressedException, "The expected suppressed exception should not be null"); Throwable[] suppressed = actual.getSuppressed(); for (int i = 0; i < suppressed.length; i++) { if (compareThrowable(suppressed[i], expectedSuppressedException)) return; } throw failures.failure(info, shouldHaveSuppressedException(actual, expectedSuppressedException)); }
@Test public void whenIterableDelayErrorPublishersVoidCombinesErrors() { Exception boom1 = new NullPointerException("boom1"); Exception boom2 = new IllegalArgumentException("boom2"); Iterable<Publisher<Void>> voidPublishers = Arrays.asList( Mono.<Void>empty(), Mono.<Void>error(boom1), Mono.<Void>error(boom2)); StepVerifier.create(Mono.whenDelayError(voidPublishers)) .verifyErrorMatches(e -> e.getMessage().equals("Multiple exceptions") && e.getSuppressed()[0] == boom1 && e.getSuppressed()[1] == boom2); }
private void printAndAssert(Throwable t, boolean checkForAssemblySuppressed) { t.printStackTrace(); assertThat(t) .isInstanceOf(IndexOutOfBoundsException.class) .hasMessage("Source emitted more than one item"); if (!checkForAssemblySuppressed) { assertThat(t).hasNoSuppressedExceptions(); } else { assertThat(t).satisfies(withSuppressed -> { assertThat(withSuppressed.getSuppressed()).hasSize(1); assertThat(withSuppressed.getSuppressed()[0]) .hasMessageStartingWith("\nAssembly trace from producer [reactor.core.publisher.MonoSingle] :") .hasMessageEndingWith("Flux.single ⇢ reactor.guide.GuideTests.scatterAndGather(GuideTests.java:944)\n"); }); } }
@Test public void whenIterableDelayErrorCombinesErrors() { Exception boom1 = new NullPointerException("boom1"); Exception boom2 = new IllegalArgumentException("boom2"); StepVerifier.create(Mono.zipDelayError( Arrays.asList(Mono.just("foo"), Mono.<String>error(boom1), Mono.<String>error(boom2)), Tuples.fn3())) .verifyErrorMatches(e -> e.getMessage().equals("Multiple exceptions") && e.getSuppressed()[0] == boom1 && e.getSuppressed()[1] == boom2); }
public void assertHasSuppressedException(AssertionInfo info, Throwable actual, Throwable expectedSuppressedException) { assertNotNull(info, actual); checkNotNull(expectedSuppressedException, "The expected suppressed exception should not be null"); Throwable[] suppressed = actual.getSuppressed(); for (int i = 0; i < suppressed.length; i++) { if (compareThrowable(suppressed[i], expectedSuppressedException)) return; } throw failures.failure(info, shouldHaveSuppressedException(actual, expectedSuppressedException)); }
@Test public void addThrowableRace() throws Exception { for (int i = 0; i < 10; i++) { final int idx = i; RaceTestUtils.race( () -> Exceptions.addThrowable(ADD_THROWABLE, ExceptionsTest.this, new IllegalStateException("boomState" + idx)), () -> Exceptions.addThrowable(ADD_THROWABLE, ExceptionsTest.this, new IllegalArgumentException("boomArg" + idx)) ); } assertThat(addThrowable.getSuppressed()) .hasSize(20); }
@Test public void errorCallbackError() { IllegalStateException err = new IllegalStateException("test"); FluxPeekFuseable<String> flux = new FluxPeekFuseable<>( Flux.error(new IllegalArgumentException("bar")), null, null, e -> { throw err; }, null, null, null, null); AssertSubscriber<String> ts = AssertSubscriber.create(); flux.subscribe(ts); ts.assertNoValues(); ts.assertError(IllegalStateException.class); ts.assertErrorWith(e -> e.getSuppressed()[0].getMessage().equals("bar")); }
@Test public void sourceFactoryAndResourceCleanupThrow() { RuntimeException sourceEx = new IllegalStateException("sourceFactory"); RuntimeException cleanupEx = new IllegalStateException("resourceCleanup"); Condition<? super Throwable> suppressingFactory = new Condition<>( e -> { Throwable[] suppressed = e.getSuppressed(); return suppressed != null && suppressed.length == 1 && suppressed[0] == sourceEx; }, "suppressing <%s>", sourceEx); Flux<String> test = new FluxUsing<>(() -> "foo", o -> { throw sourceEx; }, s -> { throw cleanupEx; }, false); StepVerifier.create(test) .verifyErrorSatisfies(e -> assertThat(e) .hasMessage("resourceCleanup") .is(suppressingFactory)); }
@Test public void testSuppressedException() { RuntimeException runtimeException = new RuntimeException(); Exception exception = new Exception(); Error error = new Error(); AutoCloseableCloser closer = AutoCloseableCloser.create(); // add twice to test self suppression handling closer.register(failingCloseable(error)); closer.register(failingCloseable(error)); closer.register(failingCloseable(exception)); closer.register(failingCloseable(exception)); closer.register(failingCloseable(runtimeException)); closer.register(failingCloseable(runtimeException)); try { closer.close(); fail("expected to fail"); } catch (Throwable t) { assertSame(t, runtimeException); assertSame(t.getSuppressed()[0], exception); assertSame(t.getSuppressed()[1], exception); assertSame(t.getSuppressed()[2], error); assertSame(t.getSuppressed()[3], error); } }