CompleatableFuture是否可以通过句柄方法异常完成?

问题描述 投票:0回答:1

我正在尝试将存储库异常映射到服务异常。这样的方法好吗?

@Override
public CompletableFuture<Void> delete(String id, Map<String, String> headers) {
  CompletableFuture<Void> future = repository.delete(id, headers);
  return future.handle((aVoid, throwable) -> {
    if (throwable != null) {
      Throwable cause = throwable.getCause();
      if (DbExcUtils.isFKViolation(throwable)) {
        future.completeExceptionally(new BadRequestException(HAS_ASSIGNED_RECORDS_MESSAGE));
      } else {
        future.completeExceptionally(cause);
      }
    }
    return aVoid;
  });
}
java concurrency completable-future
1个回答
0
投票

handle方法仅在目标handle实例完成后才调用其给定的BiFunction

© www.soinside.com 2019 - 2024. All rights reserved.