我正在编写一个自定义指令,使用cats.XorT将我的未来包装在Future [Xor [A,B]]中。我写了以下代码:
def authenticateOrRejectWithChallenge[T](authenticator: Option[HttpCredentials] ⇒ XorTFuture[T]): AuthenticationDirective[T] =
extractExecutionContext.flatMap { implicit ec ⇒
extractCredentials.flatMap { cred ⇒
onSuccess(authenticator(cred).value) flatMap {
case Xor.Right(x) => provide(x)
case Xor.Left(y:MyException) => reject(MyRejection(y)):Directive1[T]
case Xor.Left(e) =>
Directives.extractRequestContext.flatMap {
r =>
logger.error(e)(s"Unexpected authentication error when processing ${r.request}")
reject(MyRejection(MyException.fromUnexpectedThrowable(e))):Directive1[T]
}
}
}
}
我想到了以下问题: