Skip to content

Commit

Permalink
Add missing methods to set ErrorResponseConfig (zio#3072)
Browse files Browse the repository at this point in the history
  • Loading branch information
987Nabil authored Aug 30, 2024
1 parent ed4ce0f commit 16e2e2f
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions zio-http/shared/src/main/scala/zio/http/ErrorResponseConfig.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package zio.http

import zio.{FiberRef, Unsafe}
import zio._

/**
* Configuration for the response generation
Expand Down Expand Up @@ -33,10 +33,20 @@ object ErrorResponseConfig {
ErrorResponseConfig(withErrorBody = true, withStackTrace = true, maxStackTraceDepth = 0)

lazy val debug: HandlerAspect[Any, Unit] =
Middleware.runBefore(configRef.set(debugConfig))
Middleware.runBefore(setConfig(debugConfig))

def withConfig(config: ErrorResponseConfig): HandlerAspect[Any, Unit] =
Middleware.runBefore(configRef.updateSome { case oldConfig if oldConfig != config => config })
Middleware.runBefore(setConfig(config))

def setConfig(config: ErrorResponseConfig): ZIO[Any, Nothing, Unit] =
ZIO.withFiberRuntime[Any, Nothing, Unit] { (state, _) =>
val existing = state.getFiberRef(configRef)
if (existing != config) state.setFiberRef(configRef, config)
Exit.unit
}

def configLayer(config: ErrorResponseConfig): ULayer[Unit] =
ZLayer(setConfig(config))

private[http] lazy val configRef: FiberRef[ErrorResponseConfig] =
FiberRef.unsafe.make(default)(Unsafe)
Expand Down

0 comments on commit 16e2e2f

Please sign in to comment.