The partial-handler package

[ Tags: error-handling, exceptions, failure, library, mit ] [ Propose Tags ]

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 instances of the standard classes.

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 () 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, nor compose it with other handlers to form a more complex one.

Properties

Versions 0.1.0, 0.1.1, 1.0.0.0, 1.0.1, 1.0.2
Dependencies base (>=4.6 && <5) [details]
License MIT
Copyright (c) 2014, Nikita Volkov
Author Nikita Volkov <nikita.y.volkov@mail.ru>
Maintainer Nikita Volkov <nikita.y.volkov@mail.ru>
Category Exceptions, Error Handling, Failure
Home page https://github.com/nikita-volkov/partial-handler
Bug tracker https://github.com/nikita-volkov/partial-handler/issues
Source repository head: git clone git://github.com/nikita-volkov/partial-handler.git
Uploaded Tue Oct 25 11:43:45 UTC 2016 by NikitaVolkov
Distributions LTSHaskell:1.0.2, NixOS:1.0.2, Stackage:1.0.2, Tumbleweed:1.0.2
Downloads 1633 total (35 in the last 30 days)
Rating 0.0 (0 ratings) [clear rating]
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]
Hackage Matrix CI

Modules

[Index]

Downloads

Maintainer's Corner

For package maintainers and hackage trustees