[Haskell-cafe] Takusen - error handling and DBM monad
p.f.moore at gmail.com
Wed Jan 31 12:02:32 EST 2007
I'm continuing (in my occasional spare time) to try to write a "simple
database query" in takusen.
The query is OK now, but I'm struggling with error handling. I have a
simple error handler
catcher :: DBException -> DBM mark Session ()
catcher x = do
liftIO $ putStrLn $ show x
All it does is print the exception. The DBM mark Session () type, I
deduced by fiddling until something works.
Now, I can protect my query with something like
main = do
withSession (connect "..." "..." "...") ( do
-- simple query, returning reversed list of rows.
r <- doQuery (sql "select username from all_users") query1Iteratee 
liftIO $ putStrLn $ show r
So far, so good. The syntax is messy, but a little refactoring should fix that.
But this doesn't catch errors in the connect call.
Wrapping the withSession in catchDB doesn't work, as withSession is in
the IO monad , not the DBM one. But using catch with catcher
doesn't work, as that expects an error handler in the IO monad, but
catcher is in the DBM monad.
I'm getting very confused about the relationship between the DBM stuff
and the IO monad. I'm sure there are good reasons for the DBM monad,
but at the moment I'm missing them, and all I'm doing is trying random
things, to little avail.
If someone could give me a better understanding of the reasoning
behind the DBM stuff, plus some pointers on how I should be
interleaving IO and DBM stuff, I'd be hugely grateful.
Apologies if this post is vague and/or badly expressed, but I'm
currently a bit short of time, and fairly frustrated over what I feel
should be a simple task (that imperative vs functional mismatch, again
 Excuse sloppy terminology here!
More information about the Haskell-Cafe