Skip to content

Commit

Permalink
Handler factory method for functions returning Either (#3230)
Browse files Browse the repository at this point in the history
Add Handler factory method for functions returning Either

Co-authored-by: Nabil Abdel-Hafeez <[email protected]>
  • Loading branch information
jczuchnowski and 987Nabil authored Dec 14, 2024
1 parent 265f503 commit 869a230
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions zio-http/shared/src/main/scala/zio/http/Handler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,11 @@ object Handler extends HandlerPlatformSpecific with HandlerVersionSpecific {

def fromFunctionHandler[In]: FromFunctionHandler[In] = new FromFunctionHandler[In](())

/**
* Creates a Handler from an pure function from A to Either[E,B]
*/
def fromFunctionEither[In]: FromFunctionEither[In] = new FromFunctionEither[In](())

/**
* Creates a Handler from an pure function from A to HExit[R,E,B]
*/
Expand Down Expand Up @@ -1222,6 +1227,18 @@ object Handler extends HandlerPlatformSpecific with HandlerVersionSpecific {
}
}

final class FromFunctionEither[In](val self: Unit) extends AnyVal {
def apply[R, Err, Out](f: In => Either[Err, Out]): Handler[Any, Err, In, Out] =
new Handler[Any, Err, In, Out] {
override def apply(in: In): ZIO[Any, Err, Out] =
try {
Exit.fromEither(f(in))
} catch {
case error: Throwable => Exit.die(error)
}
}
}

final class FromFunctionExit[In](val self: Unit) extends AnyVal {
def apply[R, Err, Out](f: In => Exit[Err, Out]): Handler[Any, Err, In, Out] =
new Handler[Any, Err, In, Out] {
Expand Down

0 comments on commit 869a230

Please sign in to comment.