Handle errors properly for ordered updates

This commit is contained in:
Victor Denisov 2016-10-23 21:52:10 -07:00
parent f81d5ec42e
commit 316ae02ecc

View file

@ -645,16 +645,19 @@ update' ordered col updateDocs = do
updates
let lens = map length chunks
let lSums = 0 : (zipWith (+) lSums lens)
exceptionThrown <- liftIO $ newIORef False
blocks <- forM (zip lSums chunks) $ \b -> do
errorDetected <- liftIO $ newIORef False
ctx <- ask
liftIO $ do
et <- readIORef exceptionThrown
if et && ordered
then return $ UpdateResult True 0 Nothing [] [] [] -- TODO probably should be revised
else runReaderT (updateBlock ordered col b) ctx
blocks <- forM (zip lSums chunks) $ \b -> liftIO $ do
ed <- readIORef errorDetected
if ed && ordered
then return $ UpdateResult True 0 Nothing [] [] []
else do
ur <- runReaderT (updateBlock ordered col b) ctx
when (failed ur) $ do
writeIORef errorDetected True
return ur
`catch` \(e :: SomeException) -> do
writeIORef exceptionThrown True
writeIORef errorDetected True
return $ UpdateResult True 0 Nothing [] [] [] -- TODO probably should be revised
let failedTotal = or $ map failed blocks
let updatedTotal = sum $ map nMatched blocks
@ -700,8 +703,14 @@ updateBlockLegacy :: (MonadIO m)
updateBlockLegacy ordered col (prevCount, docs) = do
db <- thisDatabase
ctx <- ask
errorDetected <- liftIO $ newIORef False
results <-
liftIO $ forM (zip [prevCount, (prevCount + 1) ..] docs) $ \(i, updateDoc) -> do
ed <- readIORef errorDetected
if ed && ordered
then do
return $ UpdateResult True 0 Nothing [] [] []
else do
let doc = (at "u" updateDoc) :: Document
let sel = (at "q" updateDoc) :: Document
let upsrt = if at "upsert" updateDoc then [Upsert] else []
@ -726,11 +735,13 @@ updateBlockLegacy ordered col (prevCount, docs) = do
let c = fromMaybe defaultCode eCode
if wtimeout
then do
writeIORef errorDetected True
return $ UpdateResult True 0 Nothing [] [] [WriteConcernError c errV]
else do
writeIORef errorDetected True
return $ UpdateResult True 0 Nothing [] [WriteError i c errV] []
`catch` \(e :: SomeException) -> do
when ordered $ liftIO $ throwIO e
writeIORef errorDetected True
return $ UpdateResult True 0 Nothing [] [WriteError i 0 (show e)] []
return $ foldl1' mergeUpdateResults results
@ -859,7 +870,7 @@ deleteBlock ordered col docs = do
liftIO $ forM docs $ \deleteDoc -> do
let sel = (at "q" deleteDoc) :: Document
let opts = if at "limit" deleteDoc == (1 :: Int) then [SingleRemove] else []
runReaderT (write (Delete (db <.> col) opts sel)) ctx
_ <- runReaderT (write (Delete (db <.> col) opts sel)) ctx
return Nothing
`catch` \(e :: SomeException) -> do
when ordered $ liftIO $ throwIO e