Skip to content

Commit

Permalink
Improve error handling in constrained genFromSpec
Browse files Browse the repository at this point in the history
* use the existing error handling mechanism from constrained
* remove usage of unsafePerformIO
* remove incorrectly placed `catch` and catching of `AnyException`
  • Loading branch information
teodanciu committed Jan 9, 2025
1 parent 221eaa8 commit 1dcb89a
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions libs/constrained-generators/src/Constrained/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ module Constrained.Base where

import Control.Applicative
import Control.Arrow (first)
import Control.Exception (SomeException, catch)
import Control.Monad
import Control.Monad.Identity
import Control.Monad.Writer (Writer, runWriter, tell)
Expand All @@ -76,7 +75,6 @@ import GHC.Real
import GHC.Stack
import GHC.TypeLits
import Prettyprinter
import System.IO.Unsafe
import System.Random
import System.Random.Stateful
import Test.QuickCheck hiding (Args, Fun, forAll)
Expand Down Expand Up @@ -1285,12 +1283,10 @@ envFromPred env p = case p of
-- | A version of `genFromSpecT` that simply errors if the generator fails
genFromSpec :: forall fn a. (HasCallStack, HasSpec fn a) => Specification fn a -> Gen a
genFromSpec spec = do
res <- strictGen $ explain1 "Calling genFromSpec" $ do
r <- genFromSpecT spec
unsafePerformIO $
r `seq`
pure (pure r) `catch` \(e :: SomeException) -> pure (fatalError (pure $ show e))
errorGE $ fmap pure res
res <- do
let genT = genFromSpecT @fn @a @GE spec
catchGen genT
pure $ either (error . show . NE.toList) id res

-- | A version of `genFromSpecT` that takes a seed and a size and gives you a result
genFromSpecWithSeed ::
Expand Down

0 comments on commit 1dcb89a

Please sign in to comment.