Skip to content

Commit

Permalink
Added RejectContext to RejectHandler. (#3285)
Browse files Browse the repository at this point in the history
  • Loading branch information
Grryum authored Nov 9, 2023
1 parent f6f8bfc commit 2c26873
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package sttp.tapir.server.interceptor.reject

import sttp.tapir.model.ServerRequest
import sttp.tapir.server.interceptor.RequestResult

case class RejectContext(failure: RequestResult.Failure, request: ServerRequest)
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,29 @@ import sttp.tapir.server.interceptor.RequestResult
import sttp.tapir.server.model.ValuedEndpointOutput

trait RejectHandler[F[_]] {
def apply(failure: RequestResult.Failure)(implicit monad: MonadError[F]): F[Option[ValuedEndpointOutput[_]]]
def apply(ctx: RejectContext)(implicit monad: MonadError[F]): F[Option[ValuedEndpointOutput[_]]]
}

object RejectHandler {
def apply[F[_]](f: RequestResult.Failure => F[Option[ValuedEndpointOutput[_]]]): RejectHandler[F] = new RejectHandler[F] {
override def apply(failure: RequestResult.Failure)(implicit monad: MonadError[F]): F[Option[ValuedEndpointOutput[_]]] =
f(failure)
def apply[F[_]](f: RejectContext => F[Option[ValuedEndpointOutput[_]]]): RejectHandler[F] = new RejectHandler[F] {
override def apply(ctx: RejectContext)(implicit monad: MonadError[F]): F[Option[ValuedEndpointOutput[_]]] =
f(ctx)
}

def pure[F[_]](f: RequestResult.Failure => Option[ValuedEndpointOutput[_]]): RejectHandler[F] = new RejectHandler[F] {
override def apply(failure: RequestResult.Failure)(implicit monad: MonadError[F]): F[Option[ValuedEndpointOutput[_]]] =
monad.unit(f(failure))
def pure[F[_]](f: RejectContext => Option[ValuedEndpointOutput[_]]): RejectHandler[F] = new RejectHandler[F] {
override def apply(ctx: RejectContext)(implicit monad: MonadError[F]): F[Option[ValuedEndpointOutput[_]]] =
monad.unit(f(ctx))
}
}

case class DefaultRejectHandler[F[_]](
response: (StatusCode, String) => ValuedEndpointOutput[_],
defaultStatusCodeAndBody: Option[(StatusCode, String)]
) extends RejectHandler[F] {
override def apply(failure: RequestResult.Failure)(implicit monad: MonadError[F]): F[Option[ValuedEndpointOutput[_]]] = {
override def apply(ctx: RejectContext)(implicit monad: MonadError[F]): F[Option[ValuedEndpointOutput[_]]] = {
import DefaultRejectHandler._

val statusCodeAndBody = if (hasMethodMismatch(failure)) Some(Responses.MethodNotAllowed) else defaultStatusCodeAndBody
val statusCodeAndBody = if (hasMethodMismatch(ctx.failure)) Some(Responses.MethodNotAllowed) else defaultStatusCodeAndBody
monad.unit(statusCodeAndBody.map(response.tupled))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class RejectInterceptor[F[_]](handler: RejectHandler[F]) extends RequestIntercep
next(request, endpoints).flatMap {
case r: RequestResult.Response[B] => (r: RequestResult[B]).unit
case f: RequestResult.Failure =>
handler(f).flatMap {
handler(RejectContext(f, request)).flatMap {
case Some(value) => responder(request, value).map(RequestResult.Response(_))
case None => (f: RequestResult[B]).unit
}
Expand Down

0 comments on commit 2c26873

Please sign in to comment.