-- SPDX-FileCopyrightText: 2026 Alexandra de Wit
--
-- SPDX-License-Identifier: MIT

-- | The proxy listener's innards, exported for its specs. Importing it opts out of "Ecluse.Proxy"'s stability promise.
module Ecluse.Proxy.Internal (
    warpExceptionHook,
) where

import Katip (LogEnv, Severity (ErrorS), SimpleLogPayload, katipAddContext, logFM, runKatipContextT, sl)
import Network.Wai qualified as Wai
import Network.Wai.Handler.Warp qualified as Warp

import Ecluse.Core.Fault.Http (displayFaultT)
import Ecluse.Runtime.Log (moduleField)
import Ecluse.Runtime.Server (serverModule)

-- | Warp's exception hook over the process logger. Routine client disconnects stay out of the log, so an aborted download does not spam it.
warpExceptionHook :: LogEnv -> Maybe Wai.Request -> SomeException -> IO ()
warpExceptionHook :: LogEnv -> Maybe Request -> SomeException -> IO ()
warpExceptionHook LogEnv
logEnv Maybe Request
mRequest SomeException
err =
    Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (SomeException -> Bool
Warp.defaultShouldDisplayException SomeException
err) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
        LogEnv
-> SimpleLogPayload -> Namespace -> KatipContextT IO () -> IO ()
forall c (m :: * -> *) a.
LogItem c =>
LogEnv -> c -> Namespace -> KatipContextT m a -> m a
runKatipContextT LogEnv
logEnv (SimpleLogPayload
forall a. Monoid a => a
mempty :: SimpleLogPayload) Namespace
"server" (KatipContextT IO () -> IO ()) -> KatipContextT IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
            SimpleLogPayload -> KatipContextT IO () -> KatipContextT IO ()
forall i (m :: * -> *) a.
(LogItem i, KatipContext m) =>
i -> m a -> m a
katipAddContext SimpleLogPayload
payload (KatipContextT IO () -> KatipContextT IO ())
-> KatipContextT IO () -> KatipContextT IO ()
forall a b. (a -> b) -> a -> b
$
                Severity -> LogStr -> KatipContextT IO ()
forall (m :: * -> *).
(Applicative m, KatipContext m) =>
Severity -> LogStr -> m ()
logFM Severity
ErrorS LogStr
"a fault escaped to the server (a post-commit teardown, or warp's own connection handling)"
  where
    payload :: SimpleLogPayload
payload =
        Text -> SimpleLogPayload
moduleField Text
serverModule
            SimpleLogPayload -> SimpleLogPayload -> SimpleLogPayload
forall a. Semigroup a => a -> a -> a
<> Text -> Text -> SimpleLogPayload
forall a. ToJSON a => Text -> a -> SimpleLogPayload
sl Text
"path" (Text -> (Request -> Text) -> Maybe Request -> Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Text
"unknown" :: Text) (ByteString -> Text
forall a b. ConvertUtf8 a b => b -> a
decodeUtf8 (ByteString -> Text) -> (Request -> ByteString) -> Request -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Request -> ByteString
Wai.rawPathInfo) Maybe Request
mRequest)
            SimpleLogPayload -> SimpleLogPayload -> SimpleLogPayload
forall a. Semigroup a => a -> a -> a
<> Text -> Text -> SimpleLogPayload
forall a. ToJSON a => Text -> a -> SimpleLogPayload
sl Text
"detail" (SomeException -> Text
displayFaultT SomeException
err)