@@ -60,7 +60,7 @@ import Control.Monad
60
60
when ,
61
61
)
62
62
import Control.Monad.Reader (MonadReader , ReaderT , ask , asks , local , runReaderT )
63
- import Control.Monad.Trans (MonadIO , liftIO )
63
+ import Control.Monad.Trans (MonadIO , liftIO , lift )
64
64
import qualified Crypto.Hash.MD5 as MD5
65
65
import qualified Crypto.Hash.SHA1 as SHA1
66
66
import qualified Crypto.MAC.HMAC as HMAC
@@ -131,6 +131,7 @@ import Database.MongoDB.Internal.Protocol
131
131
pwKey ,
132
132
FlagBit (.. )
133
133
)
134
+ import Control.Monad.Trans.Except
134
135
import qualified Database.MongoDB.Internal.Protocol as P
135
136
import Database.MongoDB.Internal.Util (liftIOE , loop , true1 , (<.>) )
136
137
import System.Mem.Weak (Weak )
@@ -1279,7 +1280,7 @@ find q@Query{selection, batchSize} = do
1279
1280
dBatch <- liftIO $ requestOpMsg pipe newQr []
1280
1281
newCursor db (coll selection) batchSize dBatch
1281
1282
1282
- findCommand :: (MonadIO m , MonadFail m ) => Query -> Action m Cursor
1283
+ findCommand :: (MonadIO m ) => Query -> Action m Cursor
1283
1284
-- ^ Fetch documents satisfying query using the command "find"
1284
1285
findCommand q@ Query {.. } = do
1285
1286
pipe <- asks mongoPipe
@@ -1371,7 +1372,7 @@ defFamUpdateOpts ups = FamUpdate
1371
1372
-- Return a single updated document (@new@ option is set to @True@).
1372
1373
--
1373
1374
-- See 'findAndModifyOpts' for more options.
1374
- findAndModify :: (MonadIO m , MonadFail m )
1375
+ findAndModify :: (MonadIO m )
1375
1376
=> Query
1376
1377
-> Document -- ^ updates
1377
1378
-> Action m (Either String Document )
@@ -1386,7 +1387,7 @@ findAndModify q ups = do
1386
1387
1387
1388
-- | Run the @findAndModify@ command
1388
1389
-- (allows more options than 'findAndModify')
1389
- findAndModifyOpts :: (MonadIO m , MonadFail m )
1390
+ findAndModifyOpts :: (MonadIO m )
1390
1391
=> Query
1391
1392
-> FindAndModifyOpts
1392
1393
-> Action m (Either String (Maybe Document ))
@@ -1666,7 +1667,7 @@ isCursorClosed (Cursor _ _ var) = do
1666
1667
type Pipeline = [Document ]
1667
1668
-- ^ The Aggregate Pipeline
1668
1669
1669
- aggregate :: (MonadIO m , MonadFail m ) => Collection -> Pipeline -> Action m [Document ]
1670
+ aggregate :: (MonadIO m ) => Collection -> Pipeline -> Action m [Document ]
1670
1671
-- ^ Runs an aggregate and unpacks the result. See <http://docs.mongodb.org/manual/core/aggregation/> for details.
1671
1672
aggregate aColl agg = do
1672
1673
aggregateCursor aColl agg def >>= rest
@@ -1689,7 +1690,7 @@ aggregateCommand aColl agg AggregateConfig {..} =
1689
1690
, " allowDiskUse" =: allowDiskUse
1690
1691
]
1691
1692
1692
- aggregateCursor :: (MonadIO m , MonadFail m ) => Collection -> Pipeline -> AggregateConfig -> Action m Cursor
1693
+ aggregateCursor :: (MonadIO m ) => Collection -> Pipeline -> AggregateConfig -> Action m Cursor
1693
1694
-- ^ Runs an aggregate and unpacks the result. See <http://docs.mongodb.org/manual/core/aggregation/> for details.
1694
1695
aggregateCursor aColl agg cfg = do
1695
1696
pipe <- asks mongoPipe
@@ -1708,18 +1709,21 @@ aggregateCursor aColl agg cfg = do
1708
1709
>>= either (liftIO . throwIO . AggregateFailure ) return
1709
1710
1710
1711
getCursorFromResponse
1711
- :: (MonadIO m , MonadFail m )
1712
+ :: (MonadIO m )
1712
1713
=> Collection
1713
1714
-> Document
1714
1715
-> Action m (Either String Cursor )
1715
1716
getCursorFromResponse aColl response
1716
- | true1 " ok" response = do
1717
- cursor <- lookup " cursor" response
1718
- firstBatch <- lookup " firstBatch" cursor
1719
- cursorId <- lookup " id" cursor
1720
- db <- thisDatabase
1721
- Right <$> newCursor db aColl 0 (return $ Batch Nothing cursorId firstBatch)
1717
+ | true1 " ok" response = runExceptT $ do
1718
+ cursor <- lookup " cursor" response ?? " cursor is missing "
1719
+ firstBatch <- lookup " firstBatch" cursor ?? " firstBatch is missing "
1720
+ cursorId <- lookup " id" cursor ?? " id is missing "
1721
+ db <- lift thisDatabase
1722
+ lift $ newCursor db aColl 0 (return $ Batch Nothing cursorId firstBatch)
1722
1723
| otherwise = return $ Left $ at " errmsg" response
1724
+ where
1725
+ Nothing ?? e = throwE e
1726
+ Just a ?? _ = pure a
1723
1727
1724
1728
-- ** Group
1725
1729
0 commit comments