Skip to content

Commit

Permalink
Merge pull request #145 from slamdata/errors-and-urlencoding-update
Browse files Browse the repository at this point in the history
Update for fallible `FormURLEncoded`, don't use `Aff` error channel anymore
  • Loading branch information
garyb committed Nov 3, 2019
2 parents d5a7a9c + d710d92 commit 200bad7
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 214 deletions.
41 changes: 17 additions & 24 deletions README.md
Expand Up @@ -36,11 +36,11 @@ import Data.HTTP.Method (Method(..))
import Effect.Aff (launchAff)
import Effect.Class.Console (log)
main = launchAff $ do
res <- AX.request (AX.defaultRequest { url = "/api", method = Left GET, responseFormat = ResponseFormat.json })
case res.body of
Left err -> log $ "GET /api response failed to decode: " <> AX.printResponseFormatError err
Right json -> log $ "GET /api response: " <> J.stringify json
main = void $ launchAff $ do
result <- AX.request (AX.defaultRequest { url = "/api", method = Left GET, responseFormat = ResponseFormat.json })
case result of
Left err -> log $ "GET /api response failed to decode: " <> AX.printError err
Right response -> log $ "GET /api response: " <> J.stringify response.body
```

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

```purescript
import Affjax.RequestBody as RequestBody
main = launchAff $ do
res1 <- AX.get ResponseFormat.json "/api"
case res1.body of
Left err -> log $ "GET /api response failed to decode: " <> AX.printResponseFormatError err
Right json -> log $ "GET /api response: " <> J.stringify json
res2 <- AX.post ResponseFormat.json "/api" (RequestBody.json (J.fromString "test"))
case res2.body of
Left err -> log $ "POST /api response failed to decode: " <> AX.printResponseFormatError err
Right json -> log $ "POST /api response: " <> J.stringify json
import Data.Maybe (Maybe(..))
main = void $ launchAff $ do
result1 <- AX.get ResponseFormat.json "/api"
case result1 of
Left err -> log $ "GET /api response failed to decode: " <> AX.printError err
Right response -> log $ "GET /api response: " <> J.stringify response.body
result2 <- AX.post ResponseFormat.json "/api" (Just (RequestBody.json (J.fromString "test")))
case result2 of
Left err -> log $ "POST /api response failed to decode: " <> AX.printError err
Right response -> log $ "POST /api response: " <> J.stringify response.body
```

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

## Error handling

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.

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 _))`.

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.

## Module documentation

Module documentation is [published on Pursuit](http://pursuit.purescript.org/packages/purescript-affjax).
2 changes: 1 addition & 1 deletion bower.json
Expand Up @@ -28,7 +28,7 @@
"purescript-arraybuffer-types": "^2.0.0",
"purescript-web-xhr": "^3.0.0",
"purescript-foreign": "^5.0.0",
"purescript-form-urlencoded": "^4.0.0",
"purescript-form-urlencoded": "^5.0.0",
"purescript-http-methods": "^4.0.0",
"purescript-integers": "^4.0.0",
"purescript-math": "^2.1.1",
Expand Down

0 comments on commit 200bad7

Please sign in to comment.