throwable-exceptions

throwable-exceptions gives the exception's value constructors

https://github.com/aiya000/hs-throwable-exceptions#README.md

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

See all snapshots throwable-exceptions appears in

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

Module documentation for 0.1.0.4

: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:

:+1:

PR is welcome !