Reputation: 5648
Say I have an exception like ExitFailure
. How can I match against the different values of ExitFailure
in my handler?
import System.Exit (ExitCode(..))
import Control.Monad.Catch as Exc
value :: Either Exc.SomeException String
value = Exc.throwM . ExitFailure $ 23
valHandler :: Exc.SomeException -> Either Exc.SomeException String
valHandler e = ??
main = print $ Exc.catch value valHandler
I know valHandler e = someValue
would match against any kind of exception, but I've never seen an example, where it matches against the kind of exception.
I can't use pattern matching, because I have SomeException
at my hands, and I can't use equality checks, because SomeException
doesn't have an Eq
instance?
Upvotes: 0
Views: 127
Reputation: 48591
The Exception
class offers a method for this:
fromException :: SomeException -> Maybe e
You can use this to recover the exception.
valHandler :: Exc.SomeException -> Either Exc.SomeException String
valHandler e
| Just (ExitFailure code) <- fromException e
= Right ...
| otherwise = Left e
Upvotes: 1