1-- | Support for checked exceptions.
13import Control.Exception
18import Effectful.Internal.Effect
19import Effectful.Internal.Env
20import Effectful.Internal.Monad
22data Error e :: Effect where
23 Error :: Unique -> Error e m r
25-- | TODO: write about possibility of an error escaping the scope of 'runError'
26-- when misused with 'AsyncE'.
28 :: forall e es a. Typeable e
29 => Eff (Error e : es) a
30 -> Eff es (Either (CallStack, e) a)
31runError (Eff m) = unsafeEff $ \es0 -> mask $ \release -> do
32 -- A unique tag is picked so that different runError handlers for the same
33 -- type don't catch each other's exceptions.
36 es <- unsafeConsEnv (IdE (Error @e tag)) noRelinker es0
37 r <- tryErrorIO tag (release $ m es) `onException` unsafeTailEnv size0 es
38 _ <- unsafeTailEnv size0 es
42 :: forall e es a. (HasCallStack, Typeable e, Error e :> es)
45throwError e = readerEffectM @(Error e) $ \(IdE (Error tag)) -> unsafeEff_ $ do
46 throwIO $ WrapErr tag callStack e
49 :: forall e es a. (Typeable e, Error e :> es)
51 -> (CallStack -> e -> Eff es a)
53catchError (Eff m) handler = do
54 readerEffectM @(Error e) $ \(IdE (Error tag)) -> unsafeEff $ \es -> do
56 catchErrorIO tag (m es) $ \cs e -> do
58 unEff (handler cs e) es
61 :: forall e es a. (Typeable e, Error e :> es)
63 -> Eff es (Either (CallStack, e) a)
64tryError m = (Right <$> m) `catchError` \es e -> pure $ Left (es, e)
66----------------------------------------
69data WrapErr e = WrapErr Unique CallStack e
71instance Typeable e => Show (WrapErr e) where
72 showsPrec p (WrapErr _ cs e)
73 = showsPrec p "Effectful.Error.WrapErr ("
74 . showsPrec p (typeOf e)
77instance Typeable e => Exception (WrapErr e)
79catchErrorIO :: Typeable e => Unique -> IO a -> (CallStack -> e -> IO a) -> IO a
80catchErrorIO tag m handler = do
81 m `catch` \err@(WrapErr etag e cs) -> do
86tryErrorIO :: Typeable e => Unique -> IO a -> IO (Either (CallStack, e) a)
87tryErrorIO tag m = catchErrorIO tag (Right <$> m) $ \es e -> pure $ Left (es, e)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list