Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle defects in handled routes (#2580) #2596

Merged
merged 1 commit into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions zio-http/src/main/scala/zio/http/Route.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ sealed trait Route[-Env, +Err] { self =>
self match {
case Provided(route, env) => Provided(route.handleErrorCause(f), env)
case Augmented(route, aspect) => Augmented(route.handleErrorCause(f), aspect)
case Handled(routePattern, handler, location) => Handled(routePattern, handler, location)
case Handled(routePattern, handler, location) =>
Handled(routePattern, handler.mapErrorCause(c => f(c.asInstanceOf[Cause[Nothing]])), location)

case Unhandled(rpm, handler, zippable, location) =>
val handler2: Handler[Env, Response, Request, Response] = {
Expand Down Expand Up @@ -96,7 +97,8 @@ sealed trait Route[-Env, +Err] { self =>
self match {
case Provided(route, env) => Provided(route.handleErrorCauseZIO(f), env)
case Augmented(route, aspect) => Augmented(route.handleErrorCauseZIO(f), aspect)
case Handled(routePattern, handler, location) => Handled(routePattern, handler, location)
case Handled(routePattern, handler, location) =>
Handled(routePattern, handler.mapErrorCauseZIO(c => f(c.asInstanceOf[Cause[Nothing]])), location)

case Unhandled(rpm, handler, zippable, location) =>
val handler2: Handler[Env, Response, Request, Response] = {
Expand Down Expand Up @@ -162,7 +164,14 @@ sealed trait Route[-Env, +Err] { self =>
self match {
case Provided(route, env) => Provided(route.handleErrorRequestCause(f), env)
case Augmented(route, aspect) => Augmented(route.handleErrorRequestCause(f), aspect)
case Handled(routePattern, handler, location) => Handled(routePattern, handler, location)
case Handled(routePattern, handler, location) =>
Handled(
routePattern,
Handler.fromFunctionHandler[Request] { (req: Request) =>
handler.mapErrorCause(c => f(req, c.asInstanceOf[Cause[Nothing]]))
},
location,
)

case Unhandled(rpm, handler, zippable, location) =>
val handler2: Handler[Env, Response, Request, Response] = {
Expand Down Expand Up @@ -201,7 +210,14 @@ sealed trait Route[-Env, +Err] { self =>
self match {
case Provided(route, env) => Provided(route.handleErrorRequestCauseZIO(f), env)
case Augmented(route, aspect) => Augmented(route.handleErrorRequestCauseZIO(f), aspect)
case Handled(routePattern, handler, location) => Handled(routePattern, handler, location)
case Handled(routePattern, handler, location) =>
Handled(
routePattern,
Handler.fromFunctionHandler[Request] { (req: Request) =>
handler.mapErrorCauseZIO(c => f(req, c.asInstanceOf[Cause[Nothing]]))
},
location,
)

case Unhandled(rpm, handler, zippable, location) =>
val handler2: Handler[Env, Response, Request, Response] = {
Expand Down
40 changes: 40 additions & 0 deletions zio-http/src/test/scala/zio/http/RouteSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,46 @@ object RouteSpec extends ZIOHttpSpec {
resultWarning == "error accessing /endpoint: hmm...",
)
},
test("handleErrorCause should handle defects") {
val route = Method.GET / "endpoint" -> handler { (_: Request) => ZIO.dieMessage("hmm...") }
val errorHandled = route.handleErrorCause(_ => Response.text("error").status(Status.InternalServerError))
val request = Request.get(URL.decode("/endpoint").toOption.get)
for {
response <- errorHandled.toHttpApp.runZIO(request)
bodyString <- response.body.asString
} yield assertTrue(extractStatus(response) == Status.InternalServerError, bodyString == "error")
},
test("handleErrorCauseZIO should handle defects") {
val route = Method.GET / "endpoint" -> handler { (_: Request) => ZIO.dieMessage("hmm...") }
val errorHandled =
route.handleErrorCauseZIO(_ => ZIO.succeed(Response.text("error").status(Status.InternalServerError)))
val request = Request.get(URL.decode("/endpoint").toOption.get)
for {
response <- errorHandled.toHttpApp.runZIO(request)
bodyString <- response.body.asString
} yield assertTrue(extractStatus(response) == Status.InternalServerError, bodyString == "error")
},
test("handleErrorRequestCause should handle defects") {
val route = Method.GET / "endpoint" -> handler { (_: Request) => ZIO.dieMessage("hmm...") }
val errorHandled =
route.handleErrorRequestCause((_, _) => Response.text("error").status(Status.InternalServerError))
val request = Request.get(URL.decode("/endpoint").toOption.get)
for {
response <- errorHandled.toHttpApp.runZIO(request)
bodyString <- response.body.asString
} yield assertTrue(extractStatus(response) == Status.InternalServerError, bodyString == "error")
},
test("handleErrorRequestCauseZIO should handle defects") {
val route = Method.GET / "endpoint" -> handler { (_: Request) => ZIO.dieMessage("hmm...") }
val errorHandled = route.handleErrorRequestCauseZIO((_, _) =>
ZIO.succeed(Response.text("error").status(Status.InternalServerError)),
)
val request = Request.get(URL.decode("/endpoint").toOption.get)
for {
response <- errorHandled.toHttpApp.runZIO(request)
bodyString <- response.body.asString
} yield assertTrue(extractStatus(response) == Status.InternalServerError, bodyString == "error")
},
),
)
}
Loading