checked-exceptions 0.1.0.1 → 0.2.0.0
raw patch · 3 files changed
+21/−14 lines, 3 filesdep +exceptionsPVP ok
version bump matches the API change (PVP)
Dependencies added: exceptions
API changes (from Hackage documentation)
- Control.Monad.CheckedExcept: catchSomeException :: forall (m :: Type -> Type) (es :: [Type]). (Monad m, MonadIO m, Elem SomeException es) => CheckedExceptT es m ()
+ Control.Monad.CheckedExcept: catchSomeException :: forall (m :: Type -> Type) (es :: [Type]) a. (Monad m, MonadCatch m, Elem SomeException es) => CheckedExceptT es m a -> CheckedExceptT es m a
Files
- checked-exceptions.cabal +3/−2
- lib/Control/Monad/CheckedExcept.hs +10/−9
- test/Main.hs +8/−3
checked-exceptions.cabal view
@@ -4,7 +4,7 @@ -- http://haskell.org/cabal/users-guide/ name: checked-exceptions-version: 0.1.0.1+version: 0.2.0.0 synopsis: mtl-style checked exceptions description: A monad transformer that allows you to throw and catch a restricted set of exceptions, tracked at the type level. -- bug-reports:@@ -39,7 +39,8 @@ base >=4.12.0.0 && < 5 , mtl >= 2.0.0.0 && < 3.0.0.0 , constraints >= 0.14 && < 0.15- --+ , exceptions >= 0.10 && < 0.11+ -- typechecker plugin dependencies , ghc >=8.6.5 && <10 , ghc-tcplugins-extra >= 0.4 && < 0.6 hs-source-dirs: lib
lib/Control/Monad/CheckedExcept.hs view
@@ -54,7 +54,7 @@ ) where import Data.Functor ((<&>))-import Control.Exception (Exception(..), catch, SomeException)+import Control.Exception (Exception(..), SomeException) import Control.Monad.Except import Data.Functor.Identity import Data.Kind@@ -63,9 +63,10 @@ import Data.Constraint import Data.Typeable (Typeable, cast, eqT) import Data.Type.Equality-import Control.Monad.IO.Class (MonadIO (liftIO))+import Control.Monad.IO.Class (MonadIO) import Control.Monad.Trans (MonadTrans (..)) import Data.Constraint.Unsafe (unsafeCoerceConstraint)+import Control.Monad.Catch (MonadCatch (..)) -- | Isomorphic to t'ExceptT' over our open-union exceptions type @t'OneOf' es@. -- Because many effects systems have an t'ExceptT' analogue, this would be pretty simple to port to any effects system.@@ -186,7 +187,7 @@ applyAll :: (forall e. CheckedException e => e -> b) -> OneOf es -> b applyAll f (OneOf e) = f e --- | Catch an exception or @mempty@ (think 'pure ()' or 'Nothing').+-- | Catch an exception or @mempty@ (think @pure ()@ or @Nothing@). withOneOf :: (Elem e es, Monoid a, CheckedException e) => OneOf es -> (e -> a) -> a withOneOf e f = case fromOneOf e of Just x -> f x@@ -219,8 +220,8 @@ Elem' x (x ': xs) = 'True Elem' x (y ': xs) = Elem' x xs +-- TODO: Sometimes causes weird type errors when it doesn't propagate correctly. -- | @ type Elem x xs = Elem' x xs ~ 'True @--- Sometimes causes weird type errors when it doesn't propagate correctly. type family Elem x xs :: Constraint where Elem x xs = If (Elem' x xs)@@ -262,9 +263,9 @@ -- | Add 'SomeException' to the exceptions set. Preferably, call this before catching the checked -- exceptions so there are no surprising exceptions.-catchSomeException :: (Monad m, MonadIO m) => Elem SomeException es => CheckedExceptT es m ()-catchSomeException = do- me <- lift $ liftIO $ catch (pure Nothing) (pure . Just)+catchSomeException :: (Monad m, MonadCatch m, Elem SomeException es) => CheckedExceptT es m a -> CheckedExceptT es m a+catchSomeException ce = do+ me <- lift $ catch (Right <$> runCheckedExceptT ce) (pure . Left) case me of- Nothing -> pure ()- Just e -> throwCheckedException (e :: SomeException)+ Right a -> CheckedExceptT $ pure a+ Left e -> throwCheckedException (e :: SomeException)
test/Main.hs view
@@ -15,8 +15,11 @@ module Main where import Test.Tasty--- import Test.Tasty.HUnit--- import Control.Monad.CheckedExcept+import Test.Tasty.HUnit+import CompTest+import Control.Exception (try, SomeException(..))+import Control.Monad.CheckedExcept+import Data.Either (isRight) main :: IO () main = defaultMain tests@@ -26,7 +29,9 @@ unitTests :: TestTree unitTests = testGroup "Unit tests"- [+ [ testCase "testCE" $ do+ testCERes <- try @SomeException $ runCheckedExceptT $ runCheckedExceptStack testCE+ assertBool "testCE does not throw SomeException" (isRight testCERes) ] unwrap :: Either a b -> b