diff --git a/zio-http/src/main/scala/zio/http/netty/server/ServerInboundHandler.scala b/zio-http/src/main/scala/zio/http/netty/server/ServerInboundHandler.scala index 10e33420cb..311ba6afee 100644 --- a/zio-http/src/main/scala/zio/http/netty/server/ServerInboundHandler.scala +++ b/zio-http/src/main/scala/zio/http/netty/server/ServerInboundHandler.scala @@ -323,11 +323,13 @@ private[zio] final case class ServerInboundHandler( } None } - }.flatMap(_.getOrElse(ZIO.unit)).catchSomeCause { case cause => - ZIO.attempt( - attemptFastWrite(ctx, withDefaultErrorResponse(cause.squash)), - ) - } + }.foldCauseZIO( + cause => ZIO.attempt(attemptFastWrite(ctx, withDefaultErrorResponse(cause.squash))), + { + case None => ZIO.unit + case Some(task) => task.orElse(ZIO.attempt(ctx.close())) + }, + ) } pgm.provideEnvironment(env) diff --git a/zio-http/src/test/scala/zio/http/ServerSpec.scala b/zio-http/src/test/scala/zio/http/ServerSpec.scala index d46c52fe53..d624a6c527 100644 --- a/zio-http/src/test/scala/zio/http/ServerSpec.scala +++ b/zio-http/src/test/scala/zio/http/ServerSpec.scala @@ -371,6 +371,32 @@ object ServerSpec extends HttpRunnableSpec { .run() assertZIO(res)(equalTo("foo\nbar")) } @@ TestAspect.os(os => !os.isWindows), + test("streaming failure - known content type") { + val res = + Handler + .fromStream(ZStream.fromZIO(ZIO.attempt(throw new Exception("boom"))), 42) + .sandbox + .toHttpApp + .deploy + .body + .mapZIO(_.asString) + .run() + .exit + assertZIO(res)(failsWithA[java.io.IOException]) + } @@ TestAspect.timeout(10.seconds), + test("streaming failure - unknown content type") { + val res = + Handler + .fromStreamChunked(ZStream.fromZIO(ZIO.attempt(throw new Exception("boom")))) + .sandbox + .toHttpApp + .deploy + .body + .mapZIO(_.asString) + .run() + .exit + assertZIO(res)(failsWithA[java.io.IOException]) + } @@ TestAspect.timeout(10.seconds), suite("html")( test("body") { val res =