mapInnerRoute 不工作

mapInnerRoute is not working

我正在尝试创建路由,将捕获的异常包装到我的异常中,该异常携带登录用户的用户对象。理想情况下,我更愿意将该信息放入 ContextRequest 以便在我的 ExceptionHandler 中使用它,但它是不可变的。

我得出结论,正确的方法是使用 mapInnerRoute,如 this example 中所述。

def completeWithUserAwareException(user: User) =
  mapInnerRoute { route =>
    ctx =>
      try {
        route(ctx)
      } catch {
        case ex: Throwable =>
          ctx.fail(new ExceptionWithUser(user, 0, ex))
      }
  }

我是这样用的

val authenticatedRoutes = myAuthorization { user =>
    completeWithUserAwareException(user) {
    pathPrefix("admin") {
      (get & path("plugins")) {
          complete {
            ""
          }
      }
    }
  }
}

completeWithUserAwareException 的内容从未被调用。我怀疑这与外部路由的动态特性有关。

如何实现将用户上下文信息传递给异常处理程序的目标?

尝试

def completeWithUserAwareException(user: User) =
  handleExceptions(
    ExceptionHandler {
      case NonFatal(ex) => failWith(new ExceptionWithUser(user, 0, ex))
    })