Skip to content
Snippets Groups Projects
Commit 80670268 authored by Jens Nolte's avatar Jens Nolte
Browse files

Add implementations for ExceptionChannel


Co-authored-by: default avatarJan Beinke <git@janbeinke.com>
parent 9304725d
No related branches found
No related tags found
No related merge requests found
Pipeline #2697 passed
module Quasar.Exception.ExceptionChannel (
panicChannel,
loggingExceptionChannel,
) where
import Control.Concurrent (forkIO)
import Control.Concurrent.STM
import Control.Exception (BlockedIndefinitelyOnSTM(..))
import Control.Monad.Catch
import Debug.Trace qualified as Trace
import GHC.IO (unsafePerformIO)
import Quasar.Async.STMHelper
import Quasar.Exceptions
import Quasar.Prelude
import Quasar.Utils.ShortIO
import System.Exit (die)
{-# NOINLINE panicChannel #-}
panicChannel :: ExceptionChannel
panicChannel = unsafePerformIO newPanicChannel
newPanicChannel :: IO ExceptionChannel
newPanicChannel = do
var <- newEmptyTMVarIO
void $ forkIO $ handle (\BlockedIndefinitelyOnSTM -> pure ()) do
ex <- atomically $ readTMVar var
die $ "Panic: " <> displayException ex
pure $ ExceptionChannel $ void . tryPutTMVar var
loggingExceptionChannel :: TIOWorker -> ExceptionChannel
loggingExceptionChannel worker =
-- Logging an exception should never fail - so if it does this panics the application
ExceptionChannel \ex -> startShortIOSTM_ (logFn ex) worker panicChannel
where
logFn :: SomeException -> ShortIO ()
logFn ex = unsafeShortIO $ Trace.traceIO $ displayException ex
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment