throwable-exceptions gives the exception's value constructors

Version on this page:
LTS Haskell 18.28:
Stackage Nightly 2021-06-14:
Latest on Hackage:

See all snapshots throwable-exceptions appears in

MIT licensed by aiya000
This version can be pinned in stack with:throwable-exceptions-,1458

Module documentation for

:diamonds: throwable-exceptions :diamonds:

Build Status Hackage

throwable-exceptions gives the exception’s value constructors for your haskell project :dog:

Document is available in here

:muscle: Why we should use this ? :muscle:

The situation that we want to throw the specific exception is happened frequently, but many general exceptions are not given by base.

For example, IOException’s value constructor is not given.

import Control.Exception.Safe (MonadThrow, throwM, IOException(..), try, Exception, SomeException)
import Control.Monad.IO.Class (MonadIO, liftIO)
import Control.Monad.Trans.Either (runEitherT)

readFileOrError :: (MonadThrow m, MonadIO m) => FilePath -> m String
readFileOrError path = do
  xOrErr <- liftIO . try $ readFile path
  case xOrErr of
    Left e -> throwM . IOException $ show (e :: SomeException)
    Right a -> return a

main :: IO ()
main = do
  xOrErr <- runEitherT $ readFileOrError "Main.hs"
  case xOrErr of
    Left e -> putStrLn $ "oops: " ++ show (e :: SomeException)
    Right a -> putStrLn a

-- Result output vv
-- Data constructor not in scope: IOException :: [Char] -> e1

But you have not to define a specific exception yourself if you use this :muscle:


PR is welcome !