akka/akka-http

Exception in failed CompletionStage is always wrapped in CompletionException

开放

#1,267 创建于 2017年7月5日

 (4 条评论) (1 个反应) (0 位负责人)Scala (598 个派生)batch import
1 - triagedhelp wantedt:javat:routingt:server

仓库指标

星标
 (1,311 个星标)
PR 合并指标
 (平均合并 1天 10小时) (30 天内合并 2 个 PR)

描述

When using the completeOKWithFutureString or completeOKWithFuture directives from the javadsl, the exception in a failed CompletionStage is always wrapped in a CompletionException.

This is unfortunate, as you end up writing ExceptionHandlers like this:

    ExceptionHandler exceptionHandler = ExceptionHandler.newBuilder()
            .match(CompletionException.class, e -> {
              if (e.getCause() instanceof IllegalStateException) return complete(StatusCodes.NOT_FOUND);
              // Etc.
              else return complete(StatusCodes.INTERNAL_SERVER_ERROR);
            })
            .build();

The CompletionException doesn't appear to contain any useful information (not in the stacktrace either), perhaps we should consider unwrapping it in akka-http - though that would be a breaking change. Perhaps we could deprecate completeOKWithFuture(String) and add completeOKWithCompletionStage(String) that unwraps, as that's the native terminology anyway?

贡献者指南