Skip to content

Commit

Permalink
Log a warning in case of fatal errors (#2084)
Browse files Browse the repository at this point in the history
* Log a warning in case of fatal errors

* Format
  • Loading branch information
vigoo authored Apr 9, 2023
1 parent aa1750a commit 0d055f9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
13 changes: 12 additions & 1 deletion zio-http/src/main/scala/zio/http/Server.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ object Server {
responseCompression: Option[ResponseCompressionConfig],
requestStreaming: RequestStreaming,
maxHeaderSize: Int,
logWarningOnFatalError: Boolean,
) {
self =>

Expand Down Expand Up @@ -96,6 +97,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 `maxHeaderSize` value when encode/decode
* headers.
Expand Down Expand Up @@ -148,7 +155,8 @@ object Server {
Decompression.config.nested("request-decompression").withDefault(Config.default.requestDecompression) ++
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.int("max-header-size").withDefault(Config.default.maxHeaderSize) ++
zio.Config.boolean("log-warning-on-fatal-error").withDefault(Config.default.logWarningOnFatalError)
}.map {
case (
sslConfig,
Expand All @@ -160,6 +168,7 @@ object Server {
responseCompression,
requestStreaming,
maxHeaderSize,
logWarningOnFatalError,
) =>
Config(
sslConfig = sslConfig,
Expand All @@ -170,6 +179,7 @@ object Server {
responseCompression = responseCompression,
requestStreaming = requestStreaming,
maxHeaderSize = maxHeaderSize,
logWarningOnFatalError = logWarningOnFatalError,
)
}

Expand All @@ -182,6 +192,7 @@ object Server {
responseCompression = None,
requestStreaming = RequestStreaming.Disabled(1024 * 100),
maxHeaderSize = 8192,
logWarningOnFatalError = true,
)

final case class ResponseCompressionConfig(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,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)
Expand Down

0 comments on commit 0d055f9

Please sign in to comment.