diff --git a/zio-http/src/main/scala/zio/http/Route.scala b/zio-http/src/main/scala/zio/http/Route.scala index 6e3fae680a..125389974a 100644 --- a/zio-http/src/main/scala/zio/http/Route.scala +++ b/zio-http/src/main/scala/zio/http/Route.scala @@ -116,6 +116,33 @@ sealed trait Route[-Env, +Err] { self => Handled(rpm.routePattern, handler2, location) } + /** + * Allows the transformation of the Err type through a function allowing one + * to build up a Routes in Stages targets the Unhandled case + */ + final def mapError[Err1](fxn: Err => Err1)(implicit trace: Trace): Route[Env, Err1] = { + self match { + case Provided(route, env) => Provided(route.mapError(fxn), env) + case Augmented(route, aspect) => Augmented(route.mapError(fxn), aspect) + case Handled(routePattern, handler, location) => Handled(routePattern, handler, location) + case Unhandled(rpm, handler, zippable, location) => Unhandled(rpm, handler.mapError(fxn), zippable, location) + } + + } + + /** + * Allows the transformation of the Err type through an Effectful program + * allowing one to build up a Routes in Stages targets the Unhandled case + * only. + */ + final def mapErrorZIO[Err1](fxn: Err => ZIO[Any, Err1, Response])(implicit trace: Trace): Route[Env, Err1] = + self match { + case Provided(route, env) => Provided(route.mapErrorZIO(fxn), env) + case Augmented(route, aspect) => Augmented(route.mapErrorZIO(fxn), aspect) + case Handled(routePattern, handler, location) => Handled(routePattern, handler, location) + case Unhandled(rpm, handler, zippable, location) => Unhandled(rpm, handler.mapErrorZIO(fxn), zippable, location) + } + /** * Handles all typed errors in the route by converting them into responses, * taking into account the request that caused the error. This method can be @@ -419,4 +446,5 @@ object Route { Route.handled(rpm)(handler).toHandler } } + } diff --git a/zio-http/src/main/scala/zio/http/Routes.scala b/zio-http/src/main/scala/zio/http/Routes.scala index b8b953c871..4f0802284d 100644 --- a/zio-http/src/main/scala/zio/http/Routes.scala +++ b/zio-http/src/main/scala/zio/http/Routes.scala @@ -92,6 +92,20 @@ final class Routes[-Env, +Err] private (val routes: Chunk[zio.http.Route[Env, Er def handleErrorCauseZIO(f: Cause[Err] => ZIO[Any, Nothing, Response])(implicit trace: Trace): Routes[Env, Nothing] = new Routes(routes.map(_.handleErrorCauseZIO(f))) + /** + * Allows the transformation of the Err type through an Effectful program + * allowing one to build up a Routes in Stages delegates to the Route + */ + def mapErrorZIO[Err1](fxn: Err => ZIO[Any, Err1, Response])(implicit trace: Trace): Routes[Env, Err1] = + new Routes(routes.map(_.mapErrorZIO(fxn))) + + /** + * Allows the transformation of the Err type through a function allowing one + * to build up a Routes in Stages delegates to the Route + */ + def mapError[Err1](fxn: Err => Err1): Routes[Env, Err1] = + new Routes(routes.map(_.mapError(fxn))) + def nest(prefix: PathCodec[Unit])(implicit trace: Trace, ev: Err <:< Response): Routes[Env, Err] = new Routes(self.routes.map(_.nest(prefix)))