Skip to content

Broken composition of error handlers in routes #3432

@narma

Description

@narma

Following app

//> using dep "dev.zio::zio:2.1.16"
//> using dep "dev.zio::zio-streams:2.1.16"
//> using dep "dev.zio::zio-http:3.1.0"

import zio.*
import zio.http.*

object ZioHttpIssue extends zio.ZIOAppDefault:

  def causeHandler: Handler[Any, Nothing, Request, Response] = handler { (req: Request) => 
      for {
        hello <- ZIO.succeed("Hello")
        _ = throw new IllegalArgumentException("boom")
      } yield Response.text(hello)
  }

  def run = {
    val myroutes = causeHandler.toRoutes
    val app = myroutes
            // comment line below resolves the issue
        .handleErrorZIO(_ => ZIO.succeed(Response.text("handleErrorZIO")))
            // handleErrorCauseZIO does not work also
        .handleErrorCause(cause => Response.text("handleErrorCause")) // I expecting see this response

    ZIO.scoped {
        app.apply(Request.get("/")).tap { response =>
            ZIO.debug(response)
        }
    } *> ZIO.debug("and now for comparison - zio behaviour") *> zioPlain
    
  }


  def zioPlain = ZIO.attempt(1)
    .map(i => throw new IllegalArgumentException("boom"))
    .catchAll(_ => ZIO.succeed("catchAll"))
    .catchAllCause(cause => ZIO.succeed("catchAllCause")) // as expected we will see this
    .debug

prints

Response(InternalServerError,Headers(),Body.empty)
and now for comparison - zio behaviour
catchAllCause

But I was expecting

Response(Ok,Headers((content-type,text/plain)),Body.fromAsciiString(handleErrorCause))

for zio-http part

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions