Skip to content

Commit

Permalink
more fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
jdegoes committed Jul 24, 2023
1 parent 825e6b1 commit d03b762
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/dsl/middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ import zio.http._
// compose basic auth, request/response logging, timeouts middlewares
val composedMiddlewares = Middleware.basicAuth("user","pw") ++
Middleware.debug ++
RouteAspect.timeout(5.seconds)
RoutesAspect.timeout(5.seconds)
```

And then we can attach our composed bundle of middlewares to an Http using `@@`
Expand Down
8 changes: 7 additions & 1 deletion zio-http/src/main/scala/zio/http/Handler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,14 @@ sealed trait Handler[-R, +Err, -In, +Out] { self =>
final def mapError[Err1](f: Err => Err1)(implicit trace: Trace): Handler[R, Err1, In, Out] =
self.foldHandler(err => Handler.fail(f(err)), Handler.succeed(_))

/**
* Transforms all failures except pure interruption.
*/
final def mapErrorCause[Err2](f: Cause[Err] => Err2)(implicit trace: Trace): Handler[R, Err2, In, Out] =
self.foldCauseHandler(err => Handler.fail(f(err)), Handler.succeed(_))
self.foldCauseHandler(
err => if (err.isInterruptedOnly) Handler.failCause(err.asInstanceOf[Cause[Nothing]]) else Handler.fail(f(err)),
Handler.succeed(_),
)

/**
* Transforms the output of the handler effectfully
Expand Down
13 changes: 13 additions & 0 deletions zio-http/src/test/scala/zio/http/RoutePatternSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,19 @@ object RoutePatternSpec extends ZIOSpecDefault {

assertTrue(tree.get(Method.GET, Path("/users/123")).contains(1))
},
test("more specific beats less specific") {
var tree: Tree[Int] = RoutePattern.Tree.empty

val pattern1 = Method.ANY / "users"
val pattern2 = Method.OPTIONS / "users"
val pattern3 = Method.ANY / "users"

tree = tree.add(pattern1, 1)
tree = tree.add(pattern2, 2)
tree = tree.add(pattern3, 3)

assertTrue(tree.get(Method.OPTIONS, Path("/users")) == Chunk(2, 1, 3))
},
test("multiple routes") {
var tree: Tree[Unit] = RoutePattern.Tree.empty

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ object StaticFileServerSpec extends HttpRunnableSpec {
val tmpFile = File.createTempFile("test", "txt")
tmpFile.setReadable(false)
val res = Handler.fromFile(tmpFile).sandbox.toHttpApp.deploy.run().map(_.status)
assertZIO(res)(equalTo(Status.InternalServerError))
assertZIO(res)(equalTo(Status.Forbidden))
} @@ unix,
),
suite("invalid file")(
Expand Down
2 changes: 1 addition & 1 deletion zio-http/src/test/scala/zio/http/StaticServerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ object StaticServerSpec extends HttpRunnableSpec {
)

override def spec =
suite("Server") {
suite("StaticServerSpec") {
app
.as(
List(staticAppSpec, nonZIOSpec, throwableAppSpec, multiHeadersSpec),
Expand Down

0 comments on commit d03b762

Please sign in to comment.