/** * Creates a {@link CompletableFuture} which is completed exceptionally with the given Exception. * Alias of {@link #failed(Throwable)}. * * {@include.example io.sphere.sdk.utils.CompletableFutureUtilsTest#testFailed()} * * @param e exception for the future * @param <T> the type of the value of the success case * @return future */ public static <T> CompletableFuture<T> exceptionallyCompletedFuture(final Throwable e) { return failed(e); }
@Override public <T> CompletionStage<T> execute(final SphereRequest<T> sphereRequest) { return CompletableFutureUtils.failed(t); }
private void requestUpdateFailedStatus(final Throwable error) { if (!currentTokensOption.isPresent()) { currentAccessTokenFuture.completeExceptionally(error); } else if (lastTokenIsStillValid()) { //keep the old token } else { currentTokensOption = Optional.empty(); currentAccessTokenFuture = CompletableFutureUtils.failed(error); } authActor.tell(new AuthActorProtocol.FetchTokenFromSphereMessage());// }
private void requestUpdateFailedStatus(final Throwable error) { if (!currentTokensOption.isPresent()) { currentAccessTokenFuture.completeExceptionally(error); } else if (lastTokenIsStillValid()) { //keep the old token } else { currentTokensOption = Optional.empty(); currentAccessTokenFuture = CompletableFutureUtils.failed(error); } authActor.tell(new AuthActorProtocol.FetchTokenFromSphereMessage());// }
@Override public <T> CompletionStage<T> execute(final SphereRequest<T> sphereRequest) { final HttpRequestIntent httpRequest = sphereRequest.httpRequestIntent(); final HttpResponse httpResponse = function.apply(httpRequest); try { final T t = SphereClientImpl.parse(sphereRequest, objectMapper, sphereApiConfig, httpResponse, null); return CompletableFutureUtils.successful(t); } catch (final Exception e) { return CompletableFutureUtils.failed(e); } }
@Override public <T> CompletionStage<T> execute(final SphereRequest<T> sphereRequest) { final HttpRequestIntent httpRequest = sphereRequest.httpRequestIntent(); final HttpResponse httpResponse = function.apply(httpRequest); try { final T t = SphereClientImpl.parse(sphereRequest, objectMapper, sphereApiConfig, httpResponse, null); return CompletableFutureUtils.successful(t); } catch (final Exception e) { return CompletableFutureUtils.failed(e); } }
@Override protected void receive(final Object message) { receiveBuilder(message) .when(TokenDeliveredMessage.class, m -> { if (!currentTokensOption.isPresent() || currentTokenIsOlder(m.tokens)) { updateToken(m.tokens); } }) .when(TokenDeliveryFailedMessage.class, m -> { final boolean hasInvalidCredentials = m.cause.getCause() != null && m.cause.getCause() instanceof InvalidClientCredentialsException; if (hasInvalidCredentials) { AUTH_LOGGER.error(() -> "Invalid client credentials shutting down.", m.cause); currentAccessTokenFuture.completeExceptionally(m.cause);//in case it is still empty currentAccessTokenFuture = CompletableFutureUtils.failed(m.cause);//in case it was not empty close(); } else if (!currentTokensOption.isPresent()) { currentAccessTokenFuture.completeExceptionally(m.cause); } else if (lastTokenIsStillValid()) { //keep the old token } else { currentTokensOption = Optional.empty(); currentAccessTokenFuture = CompletableFutureUtils.failed(m.cause); } }); } }
@Override public CompletionStage<Integer> apply(final String s) { return counter.incrementAndGet() <= 2 ? CompletableFutureUtils.failed(new ServiceException(ERROR_MESSAGE)) : CompletableFuture.completedFuture(s.length()); } }
@Override public <T> CompletionStage<T> execute(final SphereRequest<T> sphereRequest) { final HttpRequestIntent httpRequest = sphereRequest.httpRequestIntent(); final HttpResponse httpResponse = function.apply(httpRequest); try { final T t = SphereClientImpl.parse(sphereRequest, objectMapper, SphereApiConfig.of("fake-project-key-for-testing", "https://createHttpTestDouble.tld"), httpResponse); return CompletableFutureUtils.successful(t); } catch (final Exception e) { return CompletableFutureUtils.failed(e); } }
@Test public void recoverFailure() throws Exception { final String actual = recover(failed(new RuntimeException()), e -> "hi").toCompletableFuture().join(); assertThat(actual).isEqualTo("hi"); }
@Override public <T> CompletionStage<T> execute(final SphereRequest<T> sphereRequest) { rejectExcutionIfClosed("Client is already closed."); try { final int ttl = 1; return tokenSupplier.get().thenComposeAsync(token -> execute(sphereRequest, token, ttl)); } catch (final Throwable throwable) { return CompletableFutureUtils.failed(throwable); } }
@Test public void testFailed() throws Exception { final RuntimeException e = new RuntimeException(); final CompletableFuture<String> failed = failed(e); assertThatThrownBy(() -> failed.join()).hasCause(e); }
private CompletionStage<BigInteger> tryGetNextNumber(final int timeToLive, final Throwable throwable) { if (timeToLive > 0 && isRecoverableException(throwable)) { final CompletionStage<BigInteger> bigIntegerCompletionStage = incrementAndGetSequenceNumber(); return CompletableFutureUtils.recoverWith(bigIntegerCompletionStage, (error) -> tryGetNextNumber(timeToLive - 1, error)); } else { return CompletableFutureUtils.failed(throwable); } }
@Override public <T> CompletionStage<T> execute(final SphereRequest<T> sphereRequest) { rejectExcutionIfClosed("Client is already closed."); try { final int ttl = 1; return tokenSupplier.get().thenComposeAsync(token -> execute(sphereRequest, token, ttl)); } catch (final Throwable throwable) { return CompletableFutureUtils.failed(throwable); } }
@Test public void recoverWithSuccessInSecond() throws Exception { final CompletableFuture<String> future = failed(new RuntimeException()); final Function<Throwable, CompletionStage<String>> recoverFunction = e -> delayedResult("hi"); final CompletableFuture<String> recoveredFuture = recoverWith(future, recoverFunction); final String actual = recoveredFuture.join(); assertThat(actual).isEqualTo("hi"); }
@Test public void onFailure() { final RuntimeException e = new RuntimeException("foo"); final CompletableFuture<String> future = failed(e); final Throwable[] state = {null}; catchThrowable(() -> CompletableFutureUtils.onFailure(future, exception -> state[0] = exception).toCompletableFuture().join()); assertThat(state[0]).isEqualTo(e); }
@Test public void testTransferResultError() throws Exception { final CompletableFuture<String> future = new CompletableFuture<>(); final RuntimeException e = new RuntimeException(); transferResult(failed(e), future); catchThrowable(() -> future.join()); assertThat(future.isCompletedExceptionally()).isTrue(); assertThat(blockForFailure(future)).isEqualTo(e); }
@Test public void sequenceErrorCase() { final RuntimeException exception = new RuntimeException("failed"); final List<CompletableFuture<Integer>> completableFutures = asList(delayedResult(1), failed(exception), successful(3)); final CompletableFuture<List<Integer>> sequence = CompletableFutureUtils.sequence(completableFutures); assertThatThrownBy(() -> sequence.join()).hasCause(exception); }