Skip to content

Instantly share code, notes, and snippets.

@Denommus
Created November 24, 2015 19:50
Show Gist options
  • Save Denommus/93acfd5241210c31a1de to your computer and use it in GitHub Desktop.
Save Denommus/93acfd5241210c31a1de to your computer and use it in GitHub Desktop.
Type-safe exceptions with monad transformers
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