[Haskell-beginners] Exception Handling with Iteratees

Felipe Almeida Lessa felipe.lessa at gmail.com
Tue Nov 22 07:42:14 CET 2011


On Tue, Nov 22, 2011 at 4:35 AM, Michael Craig <mkscrg at gmail.com> wrote:
> ... 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?

If you enable the ViewPatterns extension

    {-# LANGUAGE ViewPatterns #-}

then you can write handleErrors as

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

Cheers,

-- 
Felipe.



More information about the Beginners mailing list