git.iko.soy/distributed-process
on master help sign in
Lift Control.Exception.catches
commit24195473a4456c39243533907137ca63bab7c818 authorTim Watson <tim@rabbitmq.com> dateSat 26 Jan 2013, 17:01 (14 years ago) parent205b5ad
3 files changed+40−1
distributed-process/src/Control/Distributed/Process.hs+4 distributed-process/src/Control/Distributed/Process/Internal/Primitives.hs+20−1 distributed-process/tests/TestCH.hs+16
distributed-process/src/Control/Distributed/Process.hs+4
@@ -101,6 +101,8 @@ module Control.Distributed.Process
101101 , RegisterReply(..)
102102 -- * Exception handling
103103 , catch
104+ , Handler(..)
105+ , catches
104106 , try
105107 , mask
106108 , onException
@@ -238,6 +240,8 @@ import Control.Distributed.Process.Internal.Primitives
238240 , unClosure
239241 -- Exception handling
240242 , catch
243+ , Handler(..)
244+ , catches
241245 , try
242246 , mask
243247 , onException
distributed-process/src/Control/Distributed/Process/Internal/Primitives.hs+20−1
@@ -62,6 +62,8 @@ module Control.Distributed.Process.Internal.Primitives
6262 , unStatic
6363 -- * Exception handling
6464 , catch
65+ , Handler(..)
66+ , catches
6567 , try
6668 , mask
6769 , onException
@@ -96,7 +98,7 @@ import Control.Monad (when)
9698import Control.Monad.Reader (ask)
9799import Control.Monad.IO.Class (MonadIO, liftIO)
98100import Control.Applicative ((<$>))
99−import Control.Exception (Exception, throwIO, SomeException)
101+import Control.Exception (Exception(..), throw, throwIO, SomeException)
100102import qualified Control.Exception as Ex (catch, mask, try)
101103import Control.Distributed.Process.Internal.StrictMVar
102104 ( StrictMVar
@@ -645,6 +647,23 @@ bracket_ before after thing = bracket before (const after) (const thing)
645647finally :: Process a -> Process b -> Process a
646648finally a sequel = bracket_ (return ()) sequel a
647649
650+-- | You need this when using 'catches'
651+data Handler a = forall e . Exception e => Handler (e -> Process a)
652+
653+instance Functor Handler where
654+ fmap f (Handler h) = Handler (fmap f . h)
655+
656+-- | Lift 'Control.Exception.catches'
657+catches :: Process a -> [Handler a] -> Process a
658+catches proc handlers = proc `catch` catchesHandler handlers
659+
660+catchesHandler :: [Handler a] -> SomeException -> Process a
661+catchesHandler handlers e = foldr tryHandler (throw e) handlers
662+ where tryHandler (Handler handler) res
663+ = case fromException e of
664+ Just e' -> handler e'
665+ Nothing -> res
666+
648667--------------------------------------------------------------------------------
649668-- Auxiliary API --
650669--------------------------------------------------------------------------------
distributed-process/tests/TestCH.hs+16
@@ -31,6 +31,7 @@ import Control.Distributed.Process.Internal.Types
3131 ( NodeId(nodeAddress)
3232 , LocalNode(localEndPoint)
3333 , ProcessExitException(..)
34+ , nullProcessId
3435 )
3536import Control.Distributed.Process.Node
3637import Control.Distributed.Process.Serializable (Serializable)
@@ -1025,6 +1026,20 @@ testCatchesExit transport = do
10251026
10261027 takeMVar done
10271028
1029+testCatches :: NT.Transport -> Assertion
1030+testCatches transport = do
1031+ localNode <- newLocalNode transport initRemoteTable
1032+ done <- newEmptyMVar
1033+
1034+ _ <- forkProcess localNode $ do
1035+ node <- getSelfNode
1036+ (liftIO $ throwIO (ProcessLinkException (nullProcessId node) DiedNormal))
1037+ `catches` [
1038+ Handler (\(ProcessLinkException _ _) -> liftIO $ putMVar done ())
1039+ ]
1040+
1041+ takeMVar done
1042+
10281043testDie :: NT.Transport -> Assertion
10291044testDie transport = do
10301045 localNode <- newLocalNode transport initRemoteTable
@@ -1127,6 +1142,7 @@ tests (transport, transportInternals) = [
11271142 , testCase "Die" (testDie transport)
11281143 , testCase "PrettyExit" (testPrettyExit transport)
11291144 , testCase "CatchesExit" (testCatchesExit transport)
1145+ , testCase "Catches" (testCatches transport)
11301146 , testCase "ExitLocal" (testExitLocal transport)
11311147 , testCase "ExitRemote" (testExitRemote transport)
11321148 ]