-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathErrorSpec.hs
56 lines (49 loc) · 1.85 KB
/
ErrorSpec.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
module ErrorSpec where
import qualified Control.Exception as X
import Polysemy
import Polysemy.Async
import Polysemy.Error
import Polysemy.Resource
import Test.Hspec
newtype MyExc = MyExc String
deriving (Show, Eq)
instance X.Exception MyExc
spec :: Spec
spec = parallel $ do
describe "fromException" $ do
it "should catch exceptions" $ do
a <-
runM $ runError $ fromException @MyExc $ do
_ <- X.throwIO $ MyExc "hello"
pure ()
a `shouldBe` (Left $ MyExc "hello")
it "should not catch non-exceptions" $ do
a <-
runM $ runError @MyExc $ fromException @MyExc $ pure ()
a `shouldBe` Right ()
it "should happen before Resource" $ do
a <-
runFinal $ embedToFinal @IO $ resourceToIOFinal $ runError @MyExc $ do
onException
(fromException @MyExc $ do
_ <- X.throwIO $ MyExc "hello"
pure ()
) $ pure $ error "this exception shouldn't happen"
a `shouldBe` (Left $ MyExc "hello")
describe "errorToIOFinal" $ do
it "should catch errors only for the interpreted Error" $ do
res1 <- runFinal $ errorToIOFinal @() $ errorToIOFinal @() $ do
raise $ throw () `catch` \() -> return ()
res1 `shouldBe` Right (Right ())
res2 <- runFinal $ errorToIOFinal @() $ errorToIOFinal @() $ do
raise (throw ()) `catch` \() -> return ()
res2 `shouldBe` Left ()
it "should propagate errors thrown in 'async'" $ do
res1 <- runFinal $ errorToIOFinal @() $ asyncToIOFinal $ do
a <- async $ throw ()
await a
res1 `shouldBe` (Left () :: Either () (Maybe ()))
res2 <- runFinal $ errorToIOFinal @() $ asyncToIOFinal $ do
a <- async $ throw ()
await a `catch` \() -> return $ Just ()
res2 `shouldBe` Right (Just ())