Java Code Examples for com.spotify.futures.CompletableFutures#exceptionallyCompletedFuture()

The following examples show how to use com.spotify.futures.CompletableFutures#exceptionallyCompletedFuture() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LegacyCentralDogma.java    From centraldogma with Apache License 2.0 5 votes vote down vote up
private static <T> CompletableFuture<T> run(ThriftCall<T> call) {
    final ThriftFuture<T> future = new ThriftFuture<>();
    try {
        call.apply(future);
        return future.exceptionally(cause -> Exceptions.throwUnsafely(convertCause(cause)));
    } catch (Exception e) {
        return CompletableFutures.exceptionallyCompletedFuture(convertCause(e));
    }
}
 
Example 2
Source File: Repository.java    From centraldogma with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the {@link CompletableFuture} whose value is the absolute {@link Revision} of the
 * specified {@link Revision}.
 *
 * @deprecated Use {@link #normalizeNow(Revision)}.
 */
@Deprecated
default CompletableFuture<Revision> normalize(Revision revision) {
    try {
        return CompletableFuture.completedFuture(normalizeNow(revision));
    } catch (Exception e) {
        return CompletableFutures.exceptionallyCompletedFuture(e);
    }
}
 
Example 3
Source File: CentralDogmaExtension.java    From centraldogma with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new server, configures it with {@link #configure(CentralDogmaBuilder)},
 * and starts the server asynchronously.
 */
public final CompletableFuture<Void> startAsync() {
    // Create the root folder first if it doesn't exist.
    if (!dataDir.exists()) {
        try {
            dataDir.create();
        } catch (IOException e) {
            return CompletableFutures.exceptionallyCompletedFuture(e);
        }
    }

    return delegate.startAsync(dataDir.getRoot().toFile());
}
 
Example 4
Source File: CentralDogmaRule.java    From centraldogma with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new server, configures it with {@link #configure(CentralDogmaBuilder)},
 * and starts the server asynchronously.
 */
public final CompletableFuture<Void> startAsync() {
    // Create the root folder first if it doesn't exist.
    try {
        getRoot();
    } catch (IllegalStateException unused) {
        try {
            create();
        } catch (IOException e) {
            return CompletableFutures.exceptionallyCompletedFuture(e);
        }
    }

    return delegate.startAsync(getRoot());
}
 
Example 5
Source File: AuthenticatePlainCommand.java    From NioImapClient with Apache License 2.0 5 votes vote down vote up
public CompletableFuture<TaggedResponse> continueAfterResponse(ImapResponse imapResponse, Throwable throwable) {
  if (throwable != null) {
    return CompletableFutures.exceptionallyCompletedFuture(throwable);
  }

  String toEncode = NUL + user + NUL + password;
  String base64EncodedAuthRequest = BaseEncoding.base64().encode(toEncode.getBytes(StandardCharsets.UTF_8));
  return imapClient.send(new SimpleStringCommand(base64EncodedAuthRequest));
}
 
Example 6
Source File: AuthorizerUtil.java    From armeria with Apache License 2.0 5 votes vote down vote up
/**
 * Determines if the specified {@code data} is authorized for this service. If the result resolves
 * to {@code true}, the request is authorized, or {@code false} otherwise. If the future resolves
 * exceptionally, the request will not be authorized.
 */
static <T> CompletionStage<Boolean> authorize(Authorizer<T> authorizer, ServiceRequestContext ctx, T data) {
    try {
        final CompletionStage<Boolean> f = authorizer.authorize(ctx, data);
        if (f == null) {
            throw new NullPointerException("An " + Authorizer.class.getSimpleName() +
                                           " returned null: " + authorizer);
        }
        return f;
    } catch (Throwable cause) {
        return CompletableFutures.exceptionallyCompletedFuture(cause);
    }
}
 
Example 7
Source File: Repository.java    From centraldogma with Apache License 2.0 4 votes vote down vote up
/**
 * Merges the JSON files sequentially as specified in the {@link MergeQuery}.
 */
default <T> CompletableFuture<MergedEntry<T>> mergeFiles(Revision revision, MergeQuery<T> query) {
    requireNonNull(revision, "revision");
    requireNonNull(query, "query");

    final List<MergeSource> mergeSources = query.mergeSources();
    // Only JSON files can currently be merged.
    mergeSources.forEach(path -> validateJsonFilePath(path.path(), "path"));

    final Revision normalizedRevision;
    try {
        normalizedRevision = normalizeNow(revision);
    } catch (Exception e) {
        return CompletableFutures.exceptionallyCompletedFuture(e);
    }
    final List<CompletableFuture<Entry<?>>> entryFutures = new ArrayList<>(mergeSources.size());
    mergeSources.forEach(path -> {
        if (!path.isOptional()) {
            entryFutures.add(get(normalizedRevision, path.path()));
        } else {
            entryFutures.add(getOrNull(normalizedRevision, path.path()));
        }
    });

    final CompletableFuture<MergedEntry<?>> mergedEntryFuture = mergeEntries(entryFutures, revision,
                                                                             query);
    final CompletableFuture<MergedEntry<T>> future = new CompletableFuture<>();
    mergedEntryFuture.handle((mergedEntry, cause) -> {
        if (cause != null) {
            if (!(cause instanceof CentralDogmaException)) {
                cause = new QueryExecutionException(cause);
            }
            future.completeExceptionally(cause);
            return null;
        }
        future.complete(unsafeCast(mergedEntry));
        return null;
    });

    return future;
}