Skip to content

Commit 4ff2543

Browse files
committed
Provide more informative xhr errors
1 parent 45dcf24 commit 4ff2543

File tree

5 files changed

+34
-15
lines changed

5 files changed

+34
-15
lines changed

ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Revision history for reflex-gadt-api
22

3+
## 0.3.0.0
4+
5+
* *Breaking*: switch to a more useful xhr error response type. To retain the current behavior, you must convert `Either XhrResponse` to `Either Text`. See `Readme.lhs` for an example.
6+
37
## 0.2.2.3
48

59
* Support GHC 9.12

Readme.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,19 @@ The `Event` of responses comes, in this case, from a function that will take the
150150
```haskell
151151

152152
> responses <- case endpoint of
153-
> Left xhr -> performXhrRequests xhr (requests :: Event t (RequesterData CatApi))
153+
> Left xhr -> do
154+
> r <- performXhrRequests xhr (requests :: Event t (RequesterData CatApi))
155+
> performEvent $ ffor r $ traverseRequesterData $ \x ->
156+
> pure $ mapLeft xhrErrorToText x
154157
> Right ws -> performWebSocketRequests ws (requests :: Event t (RequesterData CatApi))
155158
> pure ()
156159
> where
160+
> mapLeft f = \case
161+
> Right a -> Right a
162+
> Left x -> Left $ f x
163+
>
164+
> xhrErrorToText = _xhrResponse_statusText . _xhrError_response
165+
>
157166

158167
```
159168

example/.obelisk/impl/github.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
"repo": "obelisk",
44
"branch": "develop",
55
"private": false,
6-
"rev": "3b618ec6e42af4a6fffe87e8b424387ca06c767f",
7-
"sha256": "17cvazh0fcb74hrqsh8zh0siyr4sjm39dkrmdj07c0ng8v4ri82w"
6+
"rev": "d420659bf7b81094921519f1f9243f7f11cc3fc2",
7+
"sha256": "1irwjc783cr8s52vcfsz21zh17p61qp6kabaxn2qpkqfy7n2g71c"
88
}

reflex-gadt-api.cabal

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
cabal-version: >=1.10
1+
cabal-version: 3.0
22
name: reflex-gadt-api
3-
version: 0.2.2.3
3+
version: 0.3.0.0
44
synopsis: Interact with a GADT API in your reflex-dom application.
55
description:
66
This package is designed to be used in full-stack Haskell applications where the API is defined as a GADT and the frontend is using reflex-dom.
77

88
bug-reports: https://github.com/reflex-frp/reflex-gadt-api/issues
9-
license: BSD3
9+
license: BSD-3-Clause
1010
license-file: LICENSE
1111
author: Obsidian Systems
1212
maintainer: [email protected]
@@ -17,7 +17,7 @@ extra-source-files:
1717
ChangeLog.md
1818
Readme.md
1919

20-
tested-with: GHC ==8.6.5 || ==8.8.4 || ==8.10.7 || ==9.0.2 || ==9.8.2 || ==9.10.1 || ==9.12.1
20+
tested-with: GHC ==8.10.7 || ==9.10 || ==9.12
2121

2222
library
2323
hs-source-dirs: src, .

src/Reflex/Dom/GadtApi/XHR.hs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE DeriveGeneric #-}
12
{-# LANGUAGE FlexibleContexts #-}
23
{-# LANGUAGE GADTs #-}
34
{-# LANGUAGE OverloadedStrings #-}
@@ -14,6 +15,7 @@ import Data.Constraint.Extras (Has, has)
1415
import Data.Functor (void)
1516
import Data.Text (Text)
1617
import qualified Data.Text.Encoding as T
18+
import GHC.Generics
1719
import Language.Javascript.JSaddle (MonadJSM)
1820
import Language.Javascript.JSaddle.Monad (runJSM, askJSM)
1921
import Reflex.Dom.Core
@@ -38,13 +40,19 @@ performXhrRequests
3840
)
3941
=> ApiEndpoint
4042
-> Event t (RequesterData api)
41-
-> m (Event t (RequesterData (Either Text)))
43+
-> m (Event t (RequesterData (Either XhrError)))
4244
performXhrRequests apiUrl req = fmap switchPromptlyDyn $ prerender (pure never) $ do
4345
performEventAsync $ ffor req $ \r yield -> do
4446
ctx <- askJSM
4547
void $ liftIO $ forkIO $ flip runJSM ctx $
4648
liftIO . yield =<< apiRequestXhr apiUrl r
4749

50+
data XhrError = XhrError
51+
{ _xhrError_request :: XhrRequest Text
52+
, _xhrError_response :: XhrResponse
53+
}
54+
deriving (Generic)
55+
4856
-- | Encodes an API request as JSON and issues an 'XhrRequest',
4957
-- and attempts to decode the response.
5058
apiRequestXhr
@@ -56,21 +64,19 @@ apiRequestXhr
5664
)
5765
=> ApiEndpoint
5866
-> RequesterData api
59-
-> m (RequesterData (Either Text))
67+
-> m (RequesterData (Either XhrError))
6068
apiRequestXhr apiUrl = traverseRequesterData $ \x ->
6169
has @FromJSON @api x $ mkRequest x
6270
where
6371
mkRequest
6472
:: (MonadJSM m, FromJSON b)
6573
=> api b
66-
-> m (Either Text b)
74+
-> m (Either XhrError b)
6775
mkRequest req = do
6876
response <- liftIO newEmptyMVar
69-
_ <- newXMLHttpRequest (postJson apiUrl req) $
70-
liftIO . putMVar response
77+
let request = postJson apiUrl req
78+
_ <- newXMLHttpRequest request $ liftIO . putMVar response
7179
xhrResp <- liftIO $ takeMVar response
7280
case decodeXhrResponse xhrResp of
73-
Nothing -> pure $ Left $
74-
"Response could not be decoded for request: " <>
75-
T.decodeUtf8 (LBS.toStrict $ encode req)
81+
Nothing -> pure $ Left $ XhrError request xhrResp
7682
Just r -> pure $ Right r

0 commit comments

Comments
 (0)