Skip to content
This repository was archived by the owner on Jul 19, 2025. It is now read-only.

Commit 08a4ec1

Browse files
committed
Replace error calls with Either Left or throwError
1 parent b794b95 commit 08a4ec1

1 file changed

Lines changed: 14 additions & 12 deletions

File tree

src/Fake.hs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module Fake (
1111
) where
1212

1313
import qualified Aeson as A
14-
import Control.Monad (forM, replicateM)
14+
import Control.Monad (replicateM)
1515
import Control.Monad.Except (ExceptT, MonadError)
1616
import qualified Control.Monad.Except as Except
1717
import Control.Monad.IO.Class (MonadIO, liftIO)
@@ -164,15 +164,17 @@ replicate num expr = do
164164
objectFromArgs :: [Expr] -> Fake Value
165165
objectFromArgs args = do
166166
let
167-
keyValuePairs = mkPairs (fmap eval args)
168-
mkPairs [] = []
169-
mkPairs [_] = error "Arguments to object must be a multiple of 2 (key + value pairs)"
170-
mkPairs (x : y : rest) = (x, y) : mkPairs rest
171-
pairs <- forM keyValuePairs (\(key, val) -> do
172-
key' <- Except.liftEither =<< A.asText <$> key
173-
val' <- val
174-
pure (key', val'))
175-
pure $ object pairs
167+
pairs = fmap (fmap mkKeyValuePair) (mkPairs args)
168+
Except.liftEither pairs >>= mapM id <&> object
169+
where
170+
mkPairs [] = Right []
171+
mkPairs [_] = Left "Arguments to object must be a multiple of 2 (key + value pairs)"
172+
mkPairs (x : y : rest) = ((x, y) :) <$> mkPairs rest
173+
mkKeyValuePair :: (Expr, Expr) -> Fake (T.Text, Value)
174+
mkKeyValuePair (key, val) = do
175+
key' <- eval key <&> A.asText >>= Except.liftEither
176+
val' <- eval val
177+
pure (key', val')
176178

177179

178180
rndListItem :: (RandomGen g, MonadState g m) => [a] -> m (Maybe a)
@@ -295,5 +297,5 @@ eval (FunctionCall "fromRegex" [pattern]) =
295297
<&> A.asText
296298
>>= Except.liftEither
297299
>>= fromRegex
298-
<&> String
299-
eval (FunctionCall name _) = error $ "No random generator for " <> T.unpack name
300+
<&> String
301+
eval (FunctionCall name _) = Except.throwError $ "No random generator for " <> T.unpack name

0 commit comments

Comments
 (0)