diff --git a/zio-http/shared/src/main/scala/zio/http/Handler.scala b/zio-http/shared/src/main/scala/zio/http/Handler.scala index 2bbdae003..e99f602c7 100644 --- a/zio-http/shared/src/main/scala/zio/http/Handler.scala +++ b/zio-http/shared/src/main/scala/zio/http/Handler.scala @@ -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] */ @@ -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] {