Skip to content

Commit 200bad7

Browse files
authored
Merge pull request #145 from slamdata/errors-and-urlencoding-update
Update for fallible `FormURLEncoded`, don't use `Aff` error channel anymore
2 parents d5a7a9c + d710d92 commit 200bad7

File tree

5 files changed

+139
-214
lines changed

5 files changed

+139
-214
lines changed

README.md

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ import Data.HTTP.Method (Method(..))
3636
import Effect.Aff (launchAff)
3737
import Effect.Class.Console (log)
3838
39-
main = launchAff $ do
40-
res <- AX.request (AX.defaultRequest { url = "/api", method = Left GET, responseFormat = ResponseFormat.json })
41-
case res.body of
42-
Left err -> log $ "GET /api response failed to decode: " <> AX.printResponseFormatError err
43-
Right json -> log $ "GET /api response: " <> J.stringify json
39+
main = void $ launchAff $ do
40+
result <- AX.request (AX.defaultRequest { url = "/api", method = Left GET, responseFormat = ResponseFormat.json })
41+
case result of
42+
Left err -> log $ "GET /api response failed to decode: " <> AX.printError err
43+
Right response -> log $ "GET /api response: " <> J.stringify response.body
4444
```
4545

4646
(`defaultRequest` is a record value that has all the required fields pre-set for convenient overriding when making a request.)
@@ -49,29 +49,22 @@ There are also a number of helpers for common `get`, `post`, `put`, `delete`, an
4949

5050
```purescript
5151
import Affjax.RequestBody as RequestBody
52-
53-
main = launchAff $ do
54-
res1 <- AX.get ResponseFormat.json "/api"
55-
case res1.body of
56-
Left err -> log $ "GET /api response failed to decode: " <> AX.printResponseFormatError err
57-
Right json -> log $ "GET /api response: " <> J.stringify json
58-
59-
res2 <- AX.post ResponseFormat.json "/api" (RequestBody.json (J.fromString "test"))
60-
case res2.body of
61-
Left err -> log $ "POST /api response failed to decode: " <> AX.printResponseFormatError err
62-
Right json -> log $ "POST /api response: " <> J.stringify json
52+
import Data.Maybe (Maybe(..))
53+
54+
main = void $ launchAff $ do
55+
result1 <- AX.get ResponseFormat.json "/api"
56+
case result1 of
57+
Left err -> log $ "GET /api response failed to decode: " <> AX.printError err
58+
Right response -> log $ "GET /api response: " <> J.stringify response.body
59+
60+
result2 <- AX.post ResponseFormat.json "/api" (Just (RequestBody.json (J.fromString "test")))
61+
case result2 of
62+
Left err -> log $ "POST /api response failed to decode: " <> AX.printError err
63+
Right response -> log $ "POST /api response: " <> J.stringify response.body
6364
```
6465

6566
See the [main module documentation](https://pursuit.purescript.org/packages/purescript-affjax/docs/Affjax) for a full list of these helpers and their variations.
6667

67-
## Error handling
68-
69-
There are two ways an Affjax request can fail: there's either some problem with the request itself, or the result that comes back is not as expected.
70-
71-
For the first case, these errors will be things like the URL being invalid or the server not existing, and will occur in the `Aff` error channel. The [`try`](https://pursuit.purescript.org/packages/purescript-aff/docs/Effect.Aff#v:try) function can lift these errors out of the error channel so the result of a request becomes `Aff (Either Error (Response _))`.
72-
73-
The latter case occurs when we did get a response for the request, but the result that came back could not be handled in the way that was expected. In these situations the `body` value of the `Response` will be a `Left` value with the error message describing what went wrong.
74-
7568
## Module documentation
7669

7770
Module documentation is [published on Pursuit](http://pursuit.purescript.org/packages/purescript-affjax).

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"purescript-arraybuffer-types": "^2.0.0",
2929
"purescript-web-xhr": "^3.0.0",
3030
"purescript-foreign": "^5.0.0",
31-
"purescript-form-urlencoded": "^4.0.0",
31+
"purescript-form-urlencoded": "^5.0.0",
3232
"purescript-http-methods": "^4.0.0",
3333
"purescript-integers": "^4.0.0",
3434
"purescript-math": "^2.1.1",

0 commit comments

Comments
 (0)