A composable exception handler
|Version on this page:||0.1.0@rev:1|
|LTS Haskell 20.24:||1.0.3|
|Stackage Nightly 2023-06-08:||1.0.3|
|Latest on Hackage:||1.0.3|
Module documentation for 0.1.0
If you have ever had to compose an exception handler for exceptions of multiple types, you know how frustraiting it can get. This library approaches this issue by providing a composable exception handler type, which has a Monoid instance.
Composability means that you can define custom partial handlers and reuse them by composing other handlers from them.
Here is an example of a composable partial handler, which only defines what to do in case of a ThreadKilled exception (the code uses the LambdaCase extension):
ignoreThreadKilled :: PartialHandler () ignoreThreadKilled = typed $ \case ThreadKilled -> Just $ return () _ -> Nothing
Here's how you can construct a handler of type
SomeException -> IO a
using this library:
totalizeRethrowing $ ignoreThreadKilled <> onAlreadyExists (putStrLn "Already exists")
and here is how you would do it traditionally (with the MultiWayIf extension):
\e -> if | Just ThreadKilled <- fromException e -> return () | Just e' <- fromException e, isAlreadyExistsError e' -> putStrLn "Already exists" | otherwise -> throwIO e
Putting all the syntactic trickery to make it shorter aside, this handler is a monolith block of code. Unlike with PartialHandler you can neither decompose it into simpler ones, or compose it with other handlers to form a more complex one.