Skip to content

Commit e2bd9a7

Browse files
committed
delete even more functions, yay
1 parent 7fddcd4 commit e2bd9a7

File tree

4 files changed

+37
-28
lines changed

4 files changed

+37
-28
lines changed

MODULES.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,6 @@ liftEff' :: forall e1 e2 a. Eff (err :: Exception | e2) a -> Aff e1 e2 (Either E
8686

8787
Lifts a synchronous computation and makes explicit any failure from exceptions.
8888

89-
#### `throw`
90-
91-
``` purescript
92-
throw :: forall e1 e2 a. Error -> Aff e1 e2 a
93-
```
94-
95-
Throws the specified exception through the error callback.
96-
9789
#### `semigroupAff`
9890

9991
``` purescript
@@ -147,4 +139,14 @@ instance monadAff :: Monad (Aff e1 e2)
147139

148140
``` purescript
149141
instance monadEffAff :: MonadEff e2 (Aff e1 e2)
150-
```
142+
```
143+
144+
145+
#### `monadErrorAff`
146+
147+
``` purescript
148+
instance monadErrorAff :: MonadError Error (Aff e1 e2)
149+
```
150+
151+
Allows users to catch and throw errors on the error channel of the
152+
asynchronous computation. See documentation in `purescript-transformers`.

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
An asynchronous effect monad for PureScript.
44

5-
The moral equivalent of `ErrorT (ContT Unit (Eff (async :: Async e1 | e2)) a`, for synchronous effects `e1`, asynchronous effects `e2`, and value `a`.
5+
The moral equivalent of `ErrorT (ContT Unit (Eff (async :: Async e1 | e2)) a`, for synchronous effects `e2`, asynchronous effects `e1`, and value `a`.
66

77
`Aff` lets you say goodbyte to monad transformers and callback hell!
88

@@ -41,7 +41,7 @@ For maximum type safety, `Aff` separates the effects of the synchronous part fro
4141

4242
Asynchronous effects are represented with an `async :: Async e1` effect label, where `e1` is the row of effects for actions to occur at some point in the future.
4343

44-
The library contains instances for `Semigroup`, `Monoid`, `Apply`, `Applicative`, `Bind`, `Monad`, and `MonadEff`. These instances allow you to compose `Aff`-ectful code as easily as `Eff`, as well as interop with existing `Eff` code.
44+
The library contains instances for `Semigroup`, `Monoid`, `Apply`, `Applicative`, `Bind`, `Monad`, `MonadEff`. and `MonadError`. These instances allow you to compose `Aff`-ectful code as easily as `Eff`, as well as interop with existing `Eff` code.
4545

4646
## Escaping Callback Hell
4747

@@ -109,15 +109,18 @@ attempt :: forall e1 e2 a. Aff e1 e2 a -> Aff e1 e2 (Either Error a)
109109

110110
This returns an `Either Error a` you can use to recover gracefully from failure.
111111

112-
The "opposite" of `attempt` is `throw`, which allows you to "throw" an exception:
112+
`Aff` has a `MonadError` instance, which comes with two functions: `catchError`, and `throwError`.
113+
114+
These are defined in [purescript-transformers](http://github.com/purescript/purescript-transformers).
115+
Here's an example of how you can use them:
113116

114117
```purescript
115-
do resp <- Ajax.get "http://foo.com"
116-
if resp.statusCode != 200 then throw myErr
118+
do resp <- (Ajax.get "http://foo.com") `catchError` (\e -> pure defaultResponse)
119+
if resp.statusCode != 200 then throwError myErr
117120
else pure resp.body
118121
```
119122

120-
Thrown exceptions are propagated on the error channel, and can be recovered from using `attempt`.
123+
Thrown exceptions are propagated on the error channel, and can be recovered from using `attempt` or `catchError`.
121124

122125
# Documentation
123126

bower.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@
1919
"package.json"
2020
],
2121
"dependencies": {
22-
"purescript-tuples": "~0.3.0",
23-
"purescript-either": "~0.1.4",
24-
"purescript-monoid": "~0.2.0",
25-
"purescript-exceptions": "~0.2.2",
26-
"purescript-control": "~0.2.2",
27-
"purescript-maybe": "~0.2.1",
28-
"purescript-monad-eff": "~0.1.0"
22+
"purescript-tuples": "~0.3.0",
23+
"purescript-either": "~0.1.4",
24+
"purescript-monoid": "~0.2.0",
25+
"purescript-exceptions": "~0.2.2",
26+
"purescript-control": "~0.2.2",
27+
"purescript-maybe": "~0.2.1",
28+
"purescript-monad-eff": "~0.1.0",
29+
"purescript-transformers": "~0.5.1"
2930
}
3031
}

src/Control/Monad/Aff.purs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ module Control.Monad.Aff
88
, liftEff'
99
, makeAff
1010
, runAff
11-
, throw
1211
)
1312
where
1413

15-
import Data.Either(Either(..))
14+
import Data.Either(Either(..), either)
1615
import Data.Monoid(Monoid, mempty)
1716
import Control.Apply
1817
import Control.Monad.Eff
1918
import Control.Monad.Eff.Exception(Error(), Exception(), catchException)
2019
import Control.Monad.Eff.Unsafe(unsafeInterleaveEff)
2120
import Control.Monad.Eff.Class
21+
import Control.Monad.Error.Class(MonadError)
2222

2323
-- | An effect constructor which accepts a row of effects. `Async e`
2424
-- | represents the effect of asynchronous computations which perform effects
@@ -64,10 +64,6 @@ module Control.Monad.Aff
6464
liftEff' :: forall e1 e2 a. Eff (err :: Exception | e2) a -> Aff e1 e2 (Either Error a)
6565
liftEff' eff = Aff (\_ f -> unsafeInterleaveEff (unsafeInterleaveEff (catchException (pure <$> Left) (Right <$> eff)) >>= f))
6666

67-
-- | Throws the specified exception through the error callback.
68-
throw :: forall e1 e2 a. Error -> Aff e1 e2 a
69-
throw e = Aff (\ex _ -> unsafeInterleaveEff (ex e))
70-
7167
instance semigroupAff :: (Semigroup a) => Semigroup (Aff e1 e2 a) where
7268
(<>) a b = (<>) <$> a <*> b
7369

@@ -90,3 +86,10 @@ module Control.Monad.Aff
9086

9187
instance monadEffAff :: MonadEff e2 (Aff e1 e2) where
9288
liftEff fa = Aff (\_ h -> unsafeInterleaveEff (unsafeInterleaveEff fa >>= h))
89+
90+
-- | Allows users to catch and throw errors on the error channel of the
91+
-- | asynchronous computation. See documentation in `purescript-transformers`.
92+
instance monadErrorAff :: MonadError Error (Aff e1 e2) where
93+
throwError e = Aff (\ex _ -> unsafeInterleaveEff (ex e))
94+
95+
catchError aff ex = attempt aff >>= either ex pure

0 commit comments

Comments
 (0)