1-- | Support for checked exceptions.
13import Control.Exception
18import Effectful.Internal.Effect
19import Effectful.Internal.Env
20import Effectful.Internal.Monad
21import Effectful.Internal.Utils
23data Error e :: Effect where
24 Error :: Unique -> Error e m r
26-- | TODO: write about possibility of an error escaping the scope of 'runError'
27-- when misused with 'AsyncE'.
29 :: forall e es a. Typeable e
30 => Eff (Error e : es) a
31 -> Eff es (Either ([String], e) a)
32runError (Eff m) = unsafeEff $ \es0 -> mask $ \release -> do
33 -- A unique tag is picked so that different runError handlers for the same
34 -- type don't catch each other's exceptions.
37 es <- unsafeConsEnv (IdE (Error @e tag)) noRelinker es0
38 r <- tryErrorIO tag (release $ m es) `onException` unsafeTailEnv size0 es
39 _ <- unsafeTailEnv size0 es
43 :: forall e es a. (HasCallStack, Typeable e, Error e :> es)
46throwError e = readerEffectM @(Error e) $ \(IdE (Error tag)) -> unsafeEff_ $ do
47 throwIO $ WrapErr tag (ppCallStack <$> getCallStack callStack) e
50 :: forall e es a. (Typeable e, Error e :> es)
52 -> ([String] -> e -> Eff es a)
54catchError (Eff m) handler = do
55 readerEffectM @(Error e) $ \(IdE (Error tag)) -> unsafeEff $ \es -> do
57 catchErrorIO tag (m es) $ \cs e -> do
59 unEff (handler cs e) es
62 :: forall e es a. (Typeable e, Error e :> es)
64 -> Eff es (Either ([String], e) a)
65tryError m = (Right <$> m) `catchError` \es e -> pure $ Left (es, e)
67----------------------------------------
70data WrapErr e = WrapErr Unique [String] e
72instance Typeable e => Show (WrapErr e) where
73 showsPrec p (WrapErr _ cs e)
74 = showsPrec p "Effectful.Error.WrapErr ("
75 . showsPrec p (typeOf e)
78instance Typeable e => Exception (WrapErr e)
80catchErrorIO :: Typeable e => Unique -> IO a -> ([String] -> e -> IO a) -> IO a
81catchErrorIO tag m handler = do
82 m `catch` \err@(WrapErr etag e cs) -> do
87tryErrorIO :: Typeable e => Unique -> IO a -> IO (Either ([String], e) a)
88tryErrorIO 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