Skip to content

Commit

Permalink
add mapError and mapErrorZIO to Routes and Route (#2568)
Browse files Browse the repository at this point in the history
add mapError and mapErrorZIO to ROutes and Route
  • Loading branch information
yisraelU authored Dec 26, 2023
1 parent 8c06d89 commit 93df80a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
28 changes: 28 additions & 0 deletions zio-http/src/main/scala/zio/http/Route.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -419,4 +446,5 @@ object Route {
Route.handled(rpm)(handler).toHandler
}
}

}
14 changes: 14 additions & 0 deletions zio-http/src/main/scala/zio/http/Routes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)))

Expand Down

0 comments on commit 93df80a

Please sign in to comment.