Created
November 24, 2015 19:50
-
-
Save Denommus/93acfd5241210c31a1de to your computer and use it in GitHub Desktop.
Type-safe exceptions with monad transformers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Control.Monad.Except | |
import Control.Exception (Exception (..), try) | |
liftErrorIO :: (Exception e, MonadIO m, MonadError e m) => IO a -> m a | |
liftErrorIO = liftIO . try >=> either throwError return | |
liftedBracket :: (Exception e, MonadIO m, MonadError e m) => m a -> (a -> m b) -> (a -> m c) -> m c | |
liftedBracket acquire release comp = do | |
resource <- acquire | |
result <- catchError (comp resource) (\e -> release resource >> throwError e) | |
_ <- release resource | |
return result | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment