[Haskell-cafe] Error handling with safer-file-handling
Florian Weimer
fw at deneb.enyo.de
Sun Nov 14 06:38:16 EST 2010
How am I supposed to write an exception handle for an invocation
of System.IO.SaferFileHandles.openFile?
Currently, I have got this:
case req of
Open path -> do
handle <-openFile (asAbsPath path) ReadMode
liftIO $ forcePut result Success
run handle
Follwing safer-file-handling-examples, I rewrote it to:
case req of
Open path -> do
(do runRegionT $ do
handle <-openFile (asAbsPath path) ReadMode
liftIO $ forcePut result Success
run handle)
`catch` \(e :: IOException) -> do
liftIO $ forcePut result $ Failure $ show e
(I would rather like to apply the handler to the openFile invocation
only, but the above matches the example code more closely.)
But this gives me a nice error message:
Handles.hs:48:17:
Could not deduce (Control.Monad.CatchIO.MonadCatchIO
(RegionT s IO))
from the context ()
arising from a use of `catch' at Handles.hs:(48,17)-(54,50)
Possible fix:
add (Control.Monad.CatchIO.MonadCatchIO
(RegionT s IO)) to the context of
the polymorphic type `forall s. RegionT s pr α'
or add an instance declaration for
(Control.Monad.CatchIO.MonadCatchIO (RegionT s IO))
In the expression:
do { (do { runRegionT
$ do { handle <- openFile (asAbsPath path) ReadMode;
.... } }) }
`catch`
\ (e :: IOException)
-> do { liftIO $ forcePut result $ Failure $ show e }
In a case alternative:
Open path
-> do { (do { runRegionT $ do { ... } }) }
`catch`
\ (e :: IOException)
-> do { liftIO $ forcePut result $ Failure $ show e }
In the expression:
case req of {
Open path
-> do { (do { runRegionT $ ... }) }
`catch`
\ (e :: IOException) -> do { ... }
Close -> return ()
_ -> liftIO $ forcePut result $ Failure "handle not open" }
safer-file-handling-examples results in similar error messages, after
applying the s/runTopRegion/runRegionT/ fix.
This is with GHC 6.12.1, with ScopedTypeVariables enabled.
More information about the Haskell-Cafe
mailing list