[Haskell-beginners] Exception Handling with Iteratees

Michael Craig mkscrg at gmail.com
Tue Nov 22 07:35:46 CET 2011


I have some exception types defined ...

    data POSTOnlyException = POSTOnlyException
        deriving ( Show, Typeable )
    instance Exception POSTOnlyException

    data BadPathException = BadPathException
        deriving ( Show, Typeable )
    instance Exception BadPathException

... and I want to use Data.Enumerator.catchError ...

    catchError :: Monad m => Iteratee a m b -> (SomeException -> Iteratee a
m b) -> Iteratee a m b

... so I define an error handler ...

    handleErrors :: SomeException -> Iteratee a m String
    handleErrors ex = case fromException ex of
        Just POSTOnlyException -> return "POSTs only!"
        Just BadPathException -> return "Bad path!"
        _ -> return "Unknown exception!"

... but of course this doesn't compile, because the types of the LHSs in
the case statement are different. I can get around it with some ugliness ...

    handleErrors :: SomeException -> Iteratee a m String
    handleErrors ex = case fromException ex of
        Just POSTOnlyException -> return "POSTs only!"
        _ -> case fromException ex of
            Just BadPathException -> return "Bad path!"
            _ -> return "Unknown exception!"

... but there must be a better way. Enlighten me?

Cheers,
Mike S Craig
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20111122/532c4765/attachment.htm>


More information about the Beginners mailing list