File tree 1 file changed +13
-5
lines changed
1 file changed +13
-5
lines changed Original file line number Diff line number Diff line change @@ -118,9 +118,11 @@ synchronous code.
118
118
## Dealing with Failure
119
119
120
120
` Aff ` has error handling baked in, so ordinarily you don't have to worry
121
- about it.
121
+ about it. For control-flow exceptions, it's advised to use ` ExceptT `
122
+ instead throwing errors in the ` Aff ` context. The support for errors
123
+ mainly exists to notify you when very bad things happen.
122
124
123
- When you need to deal with failure , you have a few options.
125
+ However, when you need to deal with ` Aff ` errors , you have a few options.
124
126
125
127
1 . ** Alt**
126
128
2 . ** MonadError**
@@ -139,15 +141,21 @@ example = do
139
141
140
142
#### 2. MonadError
141
143
142
- ` Aff ` has a ` MonadError ` instance, which comes with two functions:
143
- ` catchError ` , and ` throwError ` .
144
+ ` Aff ` has a ` MonadError ` instance, which comes with three functions:
145
+ ` try ` , ` catchError ` , and ` throwError ` .
144
146
145
147
These are defined in
146
148
[ purescript-transformers] ( http://github.com/purescript/purescript-transformers ) .
147
149
Here's an example of how you can use them:
148
150
149
151
``` purescript
150
- example = do
152
+ tryExample = do
153
+ result <- try $ Ajax.get "http://foo.com"
154
+ case result of
155
+ Left err -> pure ""
156
+ Right resp -> pure resp.body
157
+
158
+ catchThrowExample = do
151
159
resp <- Ajax.get "http://foo.com" `catchError` \_ -> pure defaultResponse
152
160
when (resp.statusCode /= 200) do
153
161
throwError myErr
You can’t perform that action at this time.
0 commit comments