packages feed

errors-ext 0.1.3 → 0.2

raw patch · 2 files changed

+11/−9 lines, 2 filesdep +exceptions

Dependencies added: exceptions

Files

errors-ext.cabal view
@@ -1,5 +1,5 @@ name: errors-ext-version: 0.1.3+version: 0.2 synopsis: Bracket-like functions for ExceptT over IO monad. description: Bracket-like functions for ExceptT over IO monad. homepage: https://github.com/A1-Triard/errors-ext#readme@@ -19,6 +19,7 @@   ghc-options: -fmax-pmcheck-iterations=100000000 -Wall -fprint-potential-instances   build-depends: base >= 4.7 && < 5                , errors+               , exceptions                , transformers   default-language: Haskell2010 @@ -32,6 +33,7 @@                , HUnit                , errors-ext                , errors+               , exceptions                , transformers   default-language: Haskell2010 
src/Control/Error/Extensions.hs view
@@ -14,7 +14,7 @@ -- limitations under the License. -- --- | This module exports bracket-like functions for 'ExceptT' over 'IO' monad.+-- | This module exports bracket-like functions for 'ExceptT'. --  module Control.Error.Extensions@@ -23,21 +23,21 @@   ) where  import Control.Error.Util-import Control.Exception import Control.Monad+import Control.Monad.Catch import Control.Monad.Trans.Class import Control.Monad.Trans.Except --- | Analogous to 'bracket', but for 'ExceptT' over 'IO'.-bracketE :: ExceptT e IO a -> (a -> ExceptT e IO b) -> (a -> ExceptT e IO c) -> ExceptT e IO c+-- | Analogous to 'bracket', but for 'ExceptT' over 'IO' (or any 'MonadMask' monad).+bracketE :: MonadMask m => ExceptT e m a -> (a -> ExceptT e m b) -> (a -> ExceptT e m c) -> ExceptT e m c bracketE acquire release action = (hoistEither =<<) $ lift $ do   resource <- runExceptT acquire-  result <- bracketOnError (return resource) (handleAll . ioRelease) ioAction+  result <- bracketOnError (return resource) (ignoreAll . ioRelease) ioAction   if isLeft result-    then handleAll (ioRelease resource) >> return result+    then ignoreAll (ioRelease resource) >> return result     else caseResult result <$> ioRelease resource   where-    handleAll = handle (\x -> let _ = x :: SomeException in return ()) . void+    ignoreAll = handleAll (const $ return ()) . void     ioAction (Left e) = return $ Left e     ioAction (Right r) = runExceptT $ action r     ioRelease (Left e) = return $ Left e@@ -47,5 +47,5 @@     caseResult (Right r) (Right _) = Right r  -- | A variant of 'bracketE' where the return value from the first computation is not required.-bracketE_ :: ExceptT e IO a -> ExceptT e IO b -> ExceptT e IO c -> ExceptT e IO c+bracketE_ :: MonadMask m => ExceptT e m a -> ExceptT e m b -> ExceptT e m c -> ExceptT e m c bracketE_ acquire release action = bracketE acquire (const release) (const action)