From 354bdade75a88056561693449fc9347c0a904e9f Mon Sep 17 00:00:00 2001 From: Daniel Vigovszky Date: Wed, 5 Apr 2023 11:08:56 +0200 Subject: [PATCH 1/2] Log a warning in case of fatal errors --- zio-http/src/main/scala/zio/http/Server.scala | 13 ++++++++++++- .../http/netty/server/ServerInboundHandler.scala | 10 +++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/zio-http/src/main/scala/zio/http/Server.scala b/zio-http/src/main/scala/zio/http/Server.scala index 1e1448423c..d3ede32fcb 100644 --- a/zio-http/src/main/scala/zio/http/Server.scala +++ b/zio-http/src/main/scala/zio/http/Server.scala @@ -55,6 +55,7 @@ object Server { responseCompression: Option[ResponseCompressionConfig], objectAggregator: Int, maxHeaderSize: Int, + logWarningOnFatalError: Boolean, ) { self => def useAggregator: Boolean = objectAggregator >= 0 @@ -102,6 +103,12 @@ object Server { */ def keepAlive(enable: Boolean): Config = self.copy(keepAlive = enable) + /** + * Log a warning in case of fatal errors when an error response cannot be + * sent back to the client + */ + def logWarningOnFatalError(enable: Boolean): Config = self.copy(logWarningOnFatalError = enable) + /** * Configure the server to use HttpObjectAggregator with the specified max * size of the aggregated content. @@ -159,7 +166,8 @@ object Server { Decompression.config.nested("request-decompression").withDefault(Config.default.requestDecompression) ++ ResponseCompressionConfig.config.nested("response-compression").optional ++ zio.Config.int("max-aggregated-request-size").withDefault(Config.default.objectAggregator) ++ - zio.Config.int("max-header-size").withDefault(Config.default.maxHeaderSize) + zio.Config.int("max-header-size").withDefault(Config.default.maxHeaderSize) ++ + zio.Config.boolean("log-warning-on-fatal-error").withDefault(Config.default.logWarningOnFatalError) }.map { case ( sslConfig, @@ -173,6 +181,7 @@ object Server { responseCompression, objectAggregator, maxHeaderSize, + logWarningOnFatalError, ) => Config( sslConfig = sslConfig, @@ -185,6 +194,7 @@ object Server { responseCompression = responseCompression, objectAggregator = objectAggregator, maxHeaderSize = maxHeaderSize, + logWarningOnFatalError = logWarningOnFatalError, ) } @@ -199,6 +209,7 @@ object Server { responseCompression = None, objectAggregator = 1024 * 100, maxHeaderSize = 8192, + logWarningOnFatalError = true, ) final case class ResponseCompressionConfig( 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 ea29c1b182..075f55706c 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 @@ -128,7 +128,15 @@ private[zio] final case class ServerInboundHandler( runtime.run(ctx, () => {}) { // We cannot return the generated response from here, but still calling the handler for its side effect // for example logging. - app.runServerErrorOrNull(Cause.die(t)).unit + app + .runServerErrorOrNull(Cause.die(t)) + .tap { response => + if (config.logWarningOnFatalError) + ZIO.logWarningCause(s"Fatal exception in Netty, cannot send error response $response", Cause.die(t)) + else + ZIO.unit + } + .unit } } super.exceptionCaught(ctx, t) From 5cbf32dec2a2d27c2bb01b59f9044d6f0e8ff003 Mon Sep 17 00:00:00 2001 From: Daniel Vigovszky Date: Sun, 9 Apr 2023 12:35:19 +0200 Subject: [PATCH 2/2] Format --- zio-http/src/main/scala/zio/http/Server.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/zio-http/src/main/scala/zio/http/Server.scala b/zio-http/src/main/scala/zio/http/Server.scala index 73fc0efbed..0dc7dd41a5 100644 --- a/zio-http/src/main/scala/zio/http/Server.scala +++ b/zio-http/src/main/scala/zio/http/Server.scala @@ -103,7 +103,7 @@ object Server { */ def logWarningOnFatalError(enable: Boolean): Config = self.copy(logWarningOnFatalError = enable) - /** + /** * Configure the server to use `maxHeaderSize` value when encode/decode * headers. */ @@ -153,7 +153,7 @@ object Server { zio.Config.boolean("accept-continue").withDefault(Config.default.acceptContinue) ++ zio.Config.boolean("keep-alive").withDefault(Config.default.keepAlive) ++ Decompression.config.nested("request-decompression").withDefault(Config.default.requestDecompression) ++ - ResponseCompressionConfig.config.nested("response-compression").optional ++ + ResponseCompressionConfig.config.nested("response-compression").optional ++ RequestStreaming.config.nested("request-streaming").withDefault(Config.default.requestStreaming) ++ zio.Config.int("max-header-size").withDefault(Config.default.maxHeaderSize) ++ zio.Config.boolean("log-warning-on-fatal-error").withDefault(Config.default.logWarningOnFatalError)