@@ -60,7 +60,7 @@ import Control.Monad
6060 when ,
6161 )
6262import Control.Monad.Reader (MonadReader , ReaderT , ask , asks , local , runReaderT )
63- import Control.Monad.Trans (MonadIO , liftIO )
63+ import Control.Monad.Trans (MonadIO , liftIO , lift )
6464import qualified Crypto.Hash.MD5 as MD5
6565import qualified Crypto.Hash.SHA1 as SHA1
6666import qualified Crypto.MAC.HMAC as HMAC
@@ -131,6 +131,7 @@ import Database.MongoDB.Internal.Protocol
131131 pwKey ,
132132 FlagBit (.. )
133133 )
134+ import Control.Monad.Trans.Except
134135import qualified Database.MongoDB.Internal.Protocol as P
135136import Database.MongoDB.Internal.Util (liftIOE , loop , true1 , (<.>) )
136137import System.Mem.Weak (Weak )
@@ -1279,7 +1280,7 @@ find q@Query{selection, batchSize} = do
12791280 dBatch <- liftIO $ requestOpMsg pipe newQr []
12801281 newCursor db (coll selection) batchSize dBatch
12811282
1282- findCommand :: (MonadIO m , MonadFail m ) => Query -> Action m Cursor
1283+ findCommand :: (MonadIO m ) => Query -> Action m Cursor
12831284-- ^ Fetch documents satisfying query using the command "find"
12841285findCommand q@ Query {.. } = do
12851286 pipe <- asks mongoPipe
@@ -1371,7 +1372,7 @@ defFamUpdateOpts ups = FamUpdate
13711372-- Return a single updated document (@new@ option is set to @True@).
13721373--
13731374-- See 'findAndModifyOpts' for more options.
1374- findAndModify :: (MonadIO m , MonadFail m )
1375+ findAndModify :: (MonadIO m )
13751376 => Query
13761377 -> Document -- ^ updates
13771378 -> Action m (Either String Document )
@@ -1386,7 +1387,7 @@ findAndModify q ups = do
13861387
13871388-- | Run the @findAndModify@ command
13881389-- (allows more options than 'findAndModify')
1389- findAndModifyOpts :: (MonadIO m , MonadFail m )
1390+ findAndModifyOpts :: (MonadIO m )
13901391 => Query
13911392 -> FindAndModifyOpts
13921393 -> Action m (Either String (Maybe Document ))
@@ -1666,7 +1667,7 @@ isCursorClosed (Cursor _ _ var) = do
16661667type Pipeline = [Document ]
16671668-- ^ The Aggregate Pipeline
16681669
1669- aggregate :: (MonadIO m , MonadFail m ) => Collection -> Pipeline -> Action m [Document ]
1670+ aggregate :: (MonadIO m ) => Collection -> Pipeline -> Action m [Document ]
16701671-- ^ Runs an aggregate and unpacks the result. See <http://docs.mongodb.org/manual/core/aggregation/> for details.
16711672aggregate aColl agg = do
16721673 aggregateCursor aColl agg def >>= rest
@@ -1689,7 +1690,7 @@ aggregateCommand aColl agg AggregateConfig {..} =
16891690 , " allowDiskUse" =: allowDiskUse
16901691 ]
16911692
1692- aggregateCursor :: (MonadIO m , MonadFail m ) => Collection -> Pipeline -> AggregateConfig -> Action m Cursor
1693+ aggregateCursor :: (MonadIO m ) => Collection -> Pipeline -> AggregateConfig -> Action m Cursor
16931694-- ^ Runs an aggregate and unpacks the result. See <http://docs.mongodb.org/manual/core/aggregation/> for details.
16941695aggregateCursor aColl agg cfg = do
16951696 pipe <- asks mongoPipe
@@ -1708,18 +1709,21 @@ aggregateCursor aColl agg cfg = do
17081709 >>= either (liftIO . throwIO . AggregateFailure ) return
17091710
17101711getCursorFromResponse
1711- :: (MonadIO m , MonadFail m )
1712+ :: (MonadIO m )
17121713 => Collection
17131714 -> Document
17141715 -> Action m (Either String Cursor )
17151716getCursorFromResponse 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)
17221723 | otherwise = return $ Left $ at " errmsg" response
1724+ where
1725+ Nothing ?? e = throwE e
1726+ Just a ?? _ = pure a
17231727
17241728-- ** Group
17251729
0 commit comments