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

Log a warning in case of fatal errors #2084

Merged
merged 3 commits into from
Apr 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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