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

Middlewares are not applied to default fallback handler #3286

Open
imRentable opened this issue Jan 29, 2025 · 0 comments
Open

Middlewares are not applied to default fallback handler #3286

imRentable opened this issue Jan 29, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@imRentable
Copy link

Describe the bug
Hi, I am unsure whether this is a bug or rather a lacking feature but I noticed that my middlewares are not taken into account if none of my routes were "hit". I.e. if a 404 occurs. To fix this, I have to add my own fallback/wildcard route. See this Scala CLI example:

//> using dep "dev.zio::zio:2.0.18"
//> using dep "dev.zio::zio-http:3.0.1"

import zio._
import zio.http._

object Demo extends ZIOAppDefault {

  val route: Route[Any, Nothing] =
    Method.GET / "foo" -> handler(Response.text("hi"))
  val routes = route.toRoutes @@ Middleware.debug

  val fallback = Route.notFound.toRoutes
  val routesWithManualFallback = route.toRoutes ++ fallback @@ Middleware.debug

  override def run: ZIO[ZIOAppArgs & Scope, Any, Any] =
    val found = Request.get("/foo")
    val notFound = Request.get("/bar")

    // won't apply the debug middleware
    routes.run(notFound).debug *>
      // will apply the debug middleware
      routesWithManualFallback.run(notFound).debug
}

Interestingly, the documentation of zio.http.RoutePattern#any (which is used in zio.http.Route#notFound) states

It is unlikely you need to use this pattern (...)

Therefore, either I am doing it wrong or this is not the intended behaviour.

Expected behaviour
Middlewares are also applied if none of the created routes have been hit.

@imRentable imRentable added the bug Something isn't working label Jan 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant