diff --git a/src/Graphula.hs b/src/Graphula.hs index 3b60d3c..1bc4b37 100755 --- a/src/Graphula.hs +++ b/src/Graphula.hs @@ -1,6 +1,8 @@ {-# LANGUAGE AllowAmbiguousTypes #-} +{-# LANGUAGE CPP #-} {-# LANGUAGE ConstraintKinds #-} {-# LANGUAGE DataKinds #-} +{-# LANGUAGE DeriveAnyClass #-} {-# LANGUAGE DerivingStrategies #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} @@ -161,15 +163,35 @@ import Graphula.Logged import Graphula.NoConstraint import Graphula.Node import System.Random (randomIO) -import Test.HUnit.Lang - ( FailureReason (..) - , HUnitFailure (..) - , formatFailureReason - ) import Test.QuickCheck (Arbitrary (..)) import Test.QuickCheck.Random (QCGen, mkQCGen) + +#if MIN_VERSION_base(4,20,0) +import Control.Exception (addExceptionContext) +import Control.Exception.Annotation (ExceptionAnnotation(..)) +import UnliftIO.Exception (mapExceptionM) + +newtype GraphulaSeed = GraphulaSeed Int + deriving stock (Show) + deriving anyclass (ExceptionAnnotation) + +addSeedContext :: MonadUnliftIO m => m a -> Int -> m a +addSeedContext f seed = mapExceptionM (addExceptionContext (GraphulaSeed seed)) f +#else +import Test.HUnit.Lang (FailureReason (..) , HUnitFailure (..) , formatFailureReason) import UnliftIO.Exception (catch, throwIO) +addSeedContext :: MonadUnliftIO m => m a -> Int -> m a +addSeedContext f seed = f `catch` logFailingSeed seed + +logFailingSeed :: MonadIO m => Int -> HUnitFailure -> m a +logFailingSeed seed = rethrowHUnitWith ("Graphula with seed: " ++ show seed) + +rethrowHUnitWith :: MonadIO m => String -> HUnitFailure -> m a +rethrowHUnitWith message (HUnitFailure l r) = + throwIO . HUnitFailure l . Reason $ message ++ "\n\n" ++ formatFailureReason r +#endif + -- | A constraint over lists of nodes for 'MonadGraphula', and 'GraphulaNode'. -- -- Helpful for defining utility functions over many nodes. @@ -260,14 +282,7 @@ runGraphulaT mSeed runDB action = do seed <- maybe (liftIO randomIO) pure mSeed qcGen <- liftIO $ newIORef $ mkQCGen seed runReaderT (runGraphulaT' action) (Args (RunDB runDB) qcGen) - `catch` logFailingSeed seed - -logFailingSeed :: MonadIO m => Int -> HUnitFailure -> m a -logFailingSeed seed = rethrowHUnitWith ("Graphula with seed: " ++ show seed) - -rethrowHUnitWith :: MonadIO m => String -> HUnitFailure -> m a -rethrowHUnitWith message (HUnitFailure l r) = - throwIO . HUnitFailure l . Reason $ message ++ "\n\n" ++ formatFailureReason r + `addSeedContext` seed type GraphulaNode m a = ( HasDependencies a