From 16e2e2f205a8bb6f7b9e549842779d525c998393 Mon Sep 17 00:00:00 2001 From: Nabil Abdel-Hafeez <7283535+987Nabil@users.noreply.github.com> Date: Sat, 31 Aug 2024 00:22:47 +0200 Subject: [PATCH] Add missing methods to set ErrorResponseConfig (#3072) --- .../scala/zio/http/ErrorResponseConfig.scala | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/zio-http/shared/src/main/scala/zio/http/ErrorResponseConfig.scala b/zio-http/shared/src/main/scala/zio/http/ErrorResponseConfig.scala index e4901a8e61..46d1cb0d38 100644 --- a/zio-http/shared/src/main/scala/zio/http/ErrorResponseConfig.scala +++ b/zio-http/shared/src/main/scala/zio/http/ErrorResponseConfig.scala @@ -1,6 +1,6 @@ package zio.http -import zio.{FiberRef, Unsafe} +import zio._ /** * Configuration for the response generation @@ -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)