Retrieving error status from Haskell MongoDB driver -


i have mongodb 1 collection user contains unique index on email:

import data.bson        (value (int32)) import database.mongodb (index (..), createindex, (=:))  createindex $ index "user" [ "email" =: int32 1 ] "email" true false 

i have written function inserts new user if email address not in use, , should fail if email address taken:

import           data.bson        (value (objid)) import           database.mongodb (action, objectid, (=:)) import qualified database.mongodb m (insert)  data user = user   { email     :: text   , firstname :: text   , lastname  :: text   } deriving show  data mongoentity = mongoentity objectid  createifnotexists :: user                   -> action handler (either text (mongoentity user)) createifnotexists (user@user {..}) =   value <- m.insert "user" [ "email" =: email                            , "firstname" =: firstname                            , "lastname" =: lastname                            ]   case value of     objid objectid -> return (right $ mongoentity objectid user)     _ -> return $ left "no document" 

i detect errors throw m.insert (such duplicate key) , return error text message.

since m.insert runs in action monad, assume sets error status if fails, can't figure out how retrieve it. assume need like

error <- geterrorstatus -- or whatever called 

immediately after value <- m.insert ... line , test error in case ... of expression.

by way, tried writing createifnotexists using findandmodify couldn't capture failure either.

i've never used module, reading documentation, best think of :

change type createifnotexists

createifnotexists :: monadio m =>                      user -> action m (either string (mongoentity user)) 

use catchjust control.exception.base. author seems discourage usage of control.monad.error.class.

main :: io () main =   pipe <- connect (host "127.0.0.1")   let user = user "jd@bar.baz" "john" "doe"   eitherentity <- catchjust writefailureerrorstr                   (access pipe master "user" $ createifnotexists user)                   (return . left)   close pipe  writefailureerrorstr  :: failure -> maybe string writefailureerrorstr (writefailure _err str) = str writefailureerrorstr _other = nothing 

you want check _err code value too, since there other reasons writefailure.

my understanding of documentation is not possible handle errors out of io.

i'm confused way of doing think i'm wrong. please show me more elegant way if there one.

more generally, think checking existence try , fail not nice. i'd rather find request , insert if no result.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -