private static Exception throwCause(Exception e, boolean combineStackTraces) throws Exception { Throwable cause = e.getCause(); if (cause == null) { throw e; } if (combineStackTraces) { StackTraceElement[] combined = ObjectArrays.concat(cause.getStackTrace(), e.getStackTrace(), StackTraceElement.class); cause.setStackTrace(combined); } if (cause instanceof Exception) { throw (Exception) cause; } if (cause instanceof Error) { throw (Error) cause; } // The cause is a weird kind of Throwable, so throw the outer exception. throw e; }
/** * Find a {@link Method} to handle the given exception. * Use {@link ExceptionDepthComparator} if more than one match is found. * @param exception the exception * @return a Method to handle the exception, or {@code null} if none found */ @Nullable public Method resolveMethod(Exception exception) { Method method = resolveMethodByExceptionType(exception.getClass()); if (method == null) { Throwable cause = exception.getCause(); if (cause != null) { method = resolveMethodByExceptionType(cause.getClass()); } } return method; }
private static <X extends Exception> X newWithCause(Class<X> exceptionClass, Throwable cause) { // getConstructors() guarantees this as long as we don't modify the array. @SuppressWarnings({"unchecked", "rawtypes"}) List<Constructor<X>> constructors = (List) Arrays.asList(exceptionClass.getConstructors()); for (Constructor<X> constructor : preferringStrings(constructors)) { @Nullable X instance = newFromConstructor(constructor, cause); if (instance != null) { if (instance.getCause() == null) { instance.initCause(cause); } return instance; } } throw new IllegalArgumentException( "No appropriate constructor for exception of type " + exceptionClass + " in response to chained exception", cause); }
@Override public void onComplete(Map<TopicPartition, OffsetAndMetadata> offsets, Exception exception) { responses.incrementAndGet(); Throwable cause = exception.getCause(); assertTrue("Unexpected exception cause type: " + (cause == null ? null : cause.getClass()), cause instanceof DisconnectException); } });
@Override public TestException apply(Exception from) { if (from instanceof ExecutionException) { return new TestException(from.getCause()); } else { assertTrue( "got " + from.getClass(), from instanceof InterruptedException || from instanceof CancellationException); return new TestException(from); } } };
@Test @Ignore("Observers can't throw") public void onErrorNotImplementedFailureSafe() { try { new SafeObserver<String>(OBSERVER_ONERROR_NOTIMPLEMENTED()).onError(new SafeObserverTestException("error!")); fail("expects exception to be thrown"); } catch (Exception e) { // assertTrue(e instanceof OnErrorNotImplementedException); assertTrue(e.getCause() instanceof SafeObserverTestException); assertEquals("error!", e.getCause().getMessage()); } }
@Test @Ignore("Observers can't throw") public void onErrorNotImplementedFailureSafe() { try { new SafeSubscriber<String>(subscriberOnErrorNotImplemented()).onError(new SafeSubscriberTestException("error!")); fail("expects exception to be thrown"); } catch (Exception e) { // assertTrue(e instanceof OnErrorNotImplementedException); assertTrue(e.getCause() instanceof SafeSubscriberTestException); assertEquals("error!", e.getCause().getMessage()); } }
@Test @Ignore("Observers can't throw") public void onCompleteSuccessWithUnsubscribeFailure() { Subscriber<String> subscriber = subscriberSuccess(); try { subscriber.onSubscribe(THROWING_DISPOSABLE); new SafeSubscriber<String>(subscriber).onComplete(); fail("expects exception to be thrown"); } catch (Exception e) { e.printStackTrace(); // FIXME no longer assertable // assertTrue(o.isUnsubscribed()); // assertTrue(e instanceof UnsubscribeFailedException); assertTrue(e.getCause() instanceof SafeSubscriberTestException); assertEquals("failure from unsubscribe", e.getMessage()); // expected since onError fails so SafeSubscriber can't help } }
@Test public void testWithoutMessage() throws Exception { final Exception exception = new CloneFailedException(generateCause()); assertNotNull(exception); assertNotNull(exception.getMessage()); final Throwable cause = exception.getCause(); assertNotNull(cause); assertEquals(WRONG_CAUSE_MESSAGE, CAUSE_MESSAGE, cause.getMessage()); } }
@Test public void testWithCauseAndMessage() throws Exception { final Exception exception = new CloneFailedException(EXCEPTION_MESSAGE, generateCause()); assertNotNull(exception); assertEquals(WRONG_EXCEPTION_MESSAGE, EXCEPTION_MESSAGE, exception.getMessage()); final Throwable cause = exception.getCause(); assertNotNull(cause); assertEquals(WRONG_CAUSE_MESSAGE, CAUSE_MESSAGE, cause.getMessage()); }
@Test public void testWithoutMessage() throws Exception { final Exception exception = new CircuitBreakingException(generateCause()); assertNotNull(exception); assertNotNull(exception.getMessage()); final Throwable cause = exception.getCause(); assertNotNull(cause); assertEquals(WRONG_CAUSE_MESSAGE, CAUSE_MESSAGE, cause.getMessage()); } }
@Test public void testWithCauseAndMessage() throws Exception { final Exception exception = new CircuitBreakingException(EXCEPTION_MESSAGE, generateCause()); assertNotNull(exception); assertEquals(WRONG_EXCEPTION_MESSAGE, EXCEPTION_MESSAGE, exception.getMessage()); final Throwable cause = exception.getCause(); assertNotNull(cause); assertEquals(WRONG_CAUSE_MESSAGE, CAUSE_MESSAGE, cause.getMessage()); }
@Test @Ignore("Observers can't throw") public void onNextOnErrorFailureSafe() { try { new SafeSubscriber<String>(OBSERVER_ONNEXT_ONERROR_FAIL()).onNext("one"); fail("expects exception to be thrown"); } catch (Exception e) { e.printStackTrace(); assertTrue(e instanceof RuntimeException); assertEquals("Error occurred when trying to propagate error to Subscriber.onError", e.getMessage()); Throwable e2 = e.getCause(); assertTrue(e2 instanceof CompositeException); List<Throwable> innerExceptions = ((CompositeException) e2).getExceptions(); assertEquals(2, innerExceptions.size()); Throwable e3 = innerExceptions.get(0); assertTrue(e3 instanceof SafeSubscriberTestException); assertEquals("onNextFail", e3.getMessage()); Throwable e4 = innerExceptions.get(1); assertTrue(e4 instanceof SafeSubscriberTestException); assertEquals("onErrorFail", e4.getMessage()); } }
@Test @Ignore("Observers can't throw") public void onNextOnErrorFailureSafe() { try { new SafeObserver<String>(OBSERVER_ONNEXT_ONERROR_FAIL()).onNext("one"); fail("expects exception to be thrown"); } catch (Exception e) { e.printStackTrace(); assertTrue(e instanceof RuntimeException); assertEquals("Error occurred when trying to propagate error to Observer.onError", e.getMessage()); Throwable e2 = e.getCause(); assertTrue(e2 instanceof CompositeException); List<Throwable> innerExceptions = ((CompositeException) e2).getExceptions(); assertEquals(2, innerExceptions.size()); Throwable e3 = innerExceptions.get(0); assertTrue(e3 instanceof SafeObserverTestException); assertEquals("onNextFail", e3.getMessage()); Throwable e4 = innerExceptions.get(1); assertTrue(e4 instanceof SafeObserverTestException); assertEquals("onErrorFail", e4.getMessage()); } }
@Test public void testSetAttributes() throws Exception { try { mBeanServer.setAttributes(objectName(countMetricName), new AttributeList(1)); fail("setAttributes should have failed"); } catch (Exception e) { assertThat(e.getCause(), instanceOf(UnsupportedOperationException.class)); } }
@Test @Ignore("Observers can't throw") public void onErrorFailureSafe() { try { new SafeSubscriber<String>(subscriberOnErrorFail()).onError(new SafeSubscriberTestException("error!")); fail("expects exception to be thrown"); } catch (Exception e) { e.printStackTrace(); assertTrue(e instanceof RuntimeException); assertEquals("Error occurred when trying to propagate error to Subscriber.onError", e.getMessage()); Throwable e2 = e.getCause(); assertTrue(e2 instanceof CompositeException); List<Throwable> innerExceptions = ((CompositeException) e2).getExceptions(); assertEquals(2, innerExceptions.size()); Throwable e3 = innerExceptions.get(0); assertTrue(e3 instanceof SafeSubscriberTestException); assertEquals("error!", e3.getMessage()); Throwable e4 = innerExceptions.get(1); assertTrue(e4 instanceof SafeSubscriberTestException); assertEquals("onErrorFail", e4.getMessage()); } }
@Test @Ignore("Observers can't throw") public void onErrorFailureSafe() { try { new SafeObserver<String>(OBSERVER_ONERROR_FAIL()).onError(new SafeObserverTestException("error!")); fail("expects exception to be thrown"); } catch (Exception e) { e.printStackTrace(); assertTrue(e instanceof RuntimeException); assertEquals("Error occurred when trying to propagate error to Observer.onError", e.getMessage()); Throwable e2 = e.getCause(); assertTrue(e2 instanceof CompositeException); List<Throwable> innerExceptions = ((CompositeException) e2).getExceptions(); assertEquals(2, innerExceptions.size()); Throwable e3 = innerExceptions.get(0); assertTrue(e3 instanceof SafeObserverTestException); assertEquals("error!", e3.getMessage()); Throwable e4 = innerExceptions.get(1); assertTrue(e4 instanceof SafeObserverTestException); assertEquals("onErrorFail", e4.getMessage()); } }
@Test @Ignore("Observers can't throw") public void onCompleteSuccessWithUnsubscribeFailure() { Observer<String> o = OBSERVER_SUCCESS(); try { o.onSubscribe(THROWING_DISPOSABLE); new SafeObserver<String>(o).onComplete(); fail("expects exception to be thrown"); } catch (Exception e) { e.printStackTrace(); // FIXME no longer assertable // assertTrue(o.isUnsubscribed()); // assertTrue(e instanceof UnsubscribeFailedException); assertTrue(e.getCause() instanceof SafeObserverTestException); assertEquals("failure from unsubscribe", e.getMessage()); // expected since onError fails so SafeObserver can't help } }
@Test public void afterCompletionWithPreSendException() { PreSendInterceptor interceptor1 = new PreSendInterceptor(); PreSendInterceptor interceptor2 = new PreSendInterceptor(); interceptor2.setExceptionToRaise(new RuntimeException("Simulated exception")); this.channel.addInterceptor(interceptor1); this.channel.addInterceptor(interceptor2); try { this.channel.send(MessageBuilder.withPayload("test").build()); } catch (Exception ex) { assertEquals("Simulated exception", ex.getCause().getMessage()); } assertTrue(interceptor1.wasAfterCompletionInvoked()); assertFalse(interceptor2.wasAfterCompletionInvoked()); }