-
Notifications
You must be signed in to change notification settings - Fork 11
feat: add seed via exception context #113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| {-# LANGUAGE AllowAmbiguousTypes #-} | ||
| {-# LANGUAGE CPP #-} | ||
| {-# LANGUAGE ConstraintKinds #-} | ||
| {-# LANGUAGE DataKinds #-} | ||
| {-# LANGUAGE DerivingStrategies #-} | ||
|
|
@@ -133,6 +134,9 @@ module Graphula | |
| , NodeOptions | ||
| , GenerateKey | ||
| , NoConstraint | ||
| #if MIN_VERSION_base(4,20,0) | ||
| , GraphulaSeed(..) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comment above deprecates this section, did you mean to add this here?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just didn't see that part of the comment. I only saw "Lower-level". I didn't realize we had deprecated re-exports here. |
||
| #endif | ||
| ) where | ||
|
|
||
| import Prelude hiding (readFile) | ||
|
|
@@ -168,7 +172,23 @@ import Test.HUnit.Lang | |
| ) | ||
| import Test.QuickCheck (Arbitrary (..)) | ||
| import Test.QuickCheck.Random (QCGen, mkQCGen) | ||
| import UnliftIO.Exception (catch, throwIO) | ||
| import UnliftIO.Exception (Exception (..), SomeException, catch, throwIO) | ||
|
|
||
| #if MIN_VERSION_base(4,20,0) | ||
| import Control.Exception (addExceptionContext) | ||
| import Control.Exception.Annotation (ExceptionAnnotation(..)) | ||
|
|
||
| newtype GraphulaSeed = GraphulaSeed Int | ||
| deriving stock (Show) | ||
|
|
||
| instance ExceptionAnnotation GraphulaSeed | ||
|
|
||
| addSeedContext :: Int -> SomeException -> SomeException | ||
| addSeedContext seed = addExceptionContext $ GraphulaSeed seed | ||
| #else | ||
| addSeedContext :: Int -> SomeException -> SomeException | ||
| addSeedContext _ = id | ||
| #endif | ||
|
|
||
| -- | A constraint over lists of nodes for 'MonadGraphula', and 'GraphulaNode'. | ||
| -- | ||
|
|
@@ -262,12 +282,24 @@ runGraphulaT mSeed runDB action = do | |
| 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) | ||
| logFailingSeed :: MonadIO m => Int -> SomeException -> m a | ||
| logFailingSeed seed = | ||
| throwIO | ||
| . addSeedContext seed | ||
| . whenException (prefixHUnitFailure ("Graphula with seed: " <> show seed)) | ||
|
|
||
| prefixHUnitFailure :: String -> HUnitFailure -> HUnitFailure | ||
| prefixHUnitFailure message (HUnitFailure l r) = | ||
| HUnitFailure l . Reason $ message ++ "\n\n" ++ formatFailureReason r | ||
|
|
||
| rethrowHUnitWith :: MonadIO m => String -> HUnitFailure -> m a | ||
| rethrowHUnitWith message (HUnitFailure l r) = | ||
| throwIO . HUnitFailure l . Reason $ message ++ "\n\n" ++ formatFailureReason r | ||
| -- | Apply a function to a 'SomeException' when it's the expected type | ||
| whenException | ||
| :: Exception e | ||
| => (e -> e) | ||
| -- ^ Function to apply if 'fromException' at this type returns 'Just' | ||
| -> SomeException | ||
| -> SomeException | ||
| whenException f ex = maybe ex (toException . f) $ fromException ex | ||
|
|
||
| type GraphulaNode m a = | ||
| ( HasDependencies a | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if the CPP guard here causes more confusion than it alleviates;
GraphulaSeedcould be defined and exported regardless of thebaseversion.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, yeah. There's also the fact that
runGraphulaTaccepts the seed as anInt. This type existing would certainly make me expect it to take aGraphulaSeed.I wonder if
GraphulaExceptionContext { seed :: Int }is better.runGraphulaTargumentGraphula.ExceptionContextmodule, to isolateCPP