Skip to content

Commit

Permalink
mongoDB: more detailed error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
gregwebs committed Sep 28, 2014
1 parent da4d304 commit 979a2be
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
25 changes: 16 additions & 9 deletions persistent-mongoDB/Database/Persist/MongoDB.hs
Original file line number Diff line number Diff line change
Expand Up @@ -880,16 +880,23 @@ fromPersistValuesThrow entDef doc =
unHaskellName (entityHaskell entDef) `mappend` ": " `mappend` t
Right entity -> return entity

mapLeft :: (a -> c) -> Either a b -> Either c b
mapLeft _ (Right r) = Right r
mapLeft f (Left l) = Left (f l)

eitherFromPersistValues :: (PersistEntity record) => EntityDef -> [DB.Field] -> Either T.Text (Entity record)
eitherFromPersistValues entDef doc =
let castDoc = assocListFromDoc doc
-- normally _id is the first field
mKey = lookup id_ castDoc
in case mKey of
Nothing -> Left "could not find _id field"
Just kpv -> fromPersistValues (map snd $ orderPersistValues (toEmbedEntityDef entDef) castDoc)
>>= \body -> keyFromValues [kpv]
>>= \key -> Right $ Entity key body
eitherFromPersistValues entDef doc = case mKey of
Nothing -> addDetail $ Left $ "could not find _id field: "
Just kpv -> do
body <- addDetail (fromPersistValues (map snd $ orderPersistValues (toEmbedEntityDef entDef) castDoc))
key <- keyFromValues [kpv]
return $ Entity key body
where
addDetail :: Either Text a -> Either Text a
addDetail = mapLeft (\msg -> msg `mappend` " for doc: " `mappend` T.pack (show doc))
castDoc = assocListFromDoc doc
-- normally _id is the first field
mKey = lookup id_ castDoc

-- | unlike many SQL databases, MongoDB makes no guarantee of the ordering
-- of the fields returned in the document.
Expand Down
2 changes: 1 addition & 1 deletion persistent-mongoDB/persistent-mongoDB.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: persistent-mongoDB
version: 2.0.8
version: 2.0.8.1
license: MIT
license-file: LICENSE
author: Greg Weber <[email protected]>
Expand Down

0 comments on commit 979a2be

Please sign in to comment.