better findAndModify error handling
This commit is contained in:
parent
f2a6307530
commit
a047c309e5
1 changed files with 14 additions and 15 deletions
|
@ -439,7 +439,7 @@ findAndModify q ups = do
|
||||||
return $ case eres of
|
return $ case eres of
|
||||||
Left l -> Left l
|
Left l -> Left l
|
||||||
Right r -> case r of
|
Right r -> case r of
|
||||||
-- mongoDB manual says this is only possible when update is True
|
-- only possible when upsert is True and new is False
|
||||||
Nothing -> Left "findAndModify: impossible null result"
|
Nothing -> Left "findAndModify: impossible null result"
|
||||||
Just doc -> Right doc
|
Just doc -> Right doc
|
||||||
|
|
||||||
|
@ -467,21 +467,20 @@ findAndModifyOpts (Query {
|
||||||
, "new" := Bool famNew -- return updated document, not original document
|
, "new" := Bool famNew -- return updated document, not original document
|
||||||
, "upsert" := Bool famUpsert -- insert if nothing is found
|
, "upsert" := Bool famUpsert -- insert if nothing is found
|
||||||
])
|
])
|
||||||
return $
|
return $ case lookupErr result of
|
||||||
case lookup "value" result of
|
|
||||||
Left err -> leftErr err
|
|
||||||
Right mdoc -> case mdoc of
|
|
||||||
Just doc@(_:_) -> case lookupErr result of
|
|
||||||
Just e -> leftErr e
|
Just e -> leftErr e
|
||||||
Nothing -> Right (Just doc)
|
Nothing -> case lookup "value" result of
|
||||||
_ -> case famOpts of
|
Left err -> leftErr $ "no document found: " `mappend` err
|
||||||
FamUpdate { famUpsert = True, famNew = True } -> Right Nothing
|
Right mdoc -> case mdoc of
|
||||||
|
Just doc@(_:_) -> Right (Just doc)
|
||||||
|
Just [] -> case famOpts of
|
||||||
|
FamUpdate { famUpsert = True, famNew = False } -> Right Nothing
|
||||||
|
_ -> leftErr $ show result
|
||||||
_ -> leftErr $ show result
|
_ -> leftErr $ show result
|
||||||
where
|
where
|
||||||
leftErr err = Left $ "findAndModify: no document found: "
|
leftErr err = Left $ "findAndModify " `mappend` show collection
|
||||||
`mappend` show collection
|
`mappend` "\nfrom query: " `mappend` show sel
|
||||||
`mappend` "from query: " `mappend` show sel
|
`mappend` "\nerror: " `mappend` err
|
||||||
`mappend` err
|
|
||||||
|
|
||||||
-- return Nothing means ok, Just is the error message
|
-- return Nothing means ok, Just is the error message
|
||||||
lookupErr result = case lookup "lastErrorObject" result of
|
lookupErr result = case lookup "lastErrorObject" result of
|
||||||
|
|
Loading…
Reference in a new issue