From 2b576ca2c8965d112647f56ed96020266247a37a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Sun, 22 Nov 2026 09:02:05 +0100 Subject: Turn exceptions to test errors --- src/Run/Monad.hs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'src/Run') diff --git a/src/Run/Monad.hs b/src/Run/Monad.hs index 0651792..3fda255 100644 --- a/src/Run/Monad.hs +++ b/src/Run/Monad.hs @@ -16,6 +16,7 @@ module Run.Monad ( import Control.Concurrent import Control.Concurrent.STM +import Control.Exception qualified as CE import Control.Monad import Control.Monad.Except import Control.Monad.Reader @@ -39,7 +40,6 @@ newtype TestRun a = TestRun { fromTestRun :: ReaderT ( SingleTestEnv, TestState ( Functor, Applicative, Monad , MonadReader ( SingleTestEnv, TestState ) , MonadWriter [ SomeObject TestRun ] - , MonadIO ) data GlobalTestEnv = GlobalTestEnv @@ -122,6 +122,20 @@ instance MonadError Failed TestRun where catchError (TestRun act) handler = TestRun $ catchError act $ fromTestRun . handler +instance MonadIO TestRun where + liftIO act = do + TestRun (liftIO (CE.try @CE.SomeException act)) >>= \case + Right x -> return x + Left err + | Just async <- CE.fromException @CE.SomeAsyncException err + -> TestRun (liftIO (CE.throwIO async)) + | otherwise + -> do + -- avoid call to this liftIO and potential throw-catch cycle from the write + out <- getOutput + TestRun (liftIO (flip runReaderT out $ outLine OutputError Nothing $ T.pack $ show err)) + throwError Failed + instance MonadEval TestRun where askGlobalDefs = asks (tsGlobals . snd) askDictionary = asks (tsLocals . snd) @@ -130,9 +144,6 @@ instance MonadEval TestRun where instance MonadOutput TestRun where getOutput = asks $ teOutput . teGlobal . fst -instance MonadPIO TestRun where - postpone = liftIO - finally :: MonadError e m => m a -> m b -> m a finally act handler = do -- cgit v1.2.3