@@ -30,7 +30,7 @@ import Data.Either (Either(..), either, note)
3030import Data.Foldable (any )
3131import Data.FormURLEncoded as FormURLEncoded
3232import Data.Function (on )
33- import Data.Function.Uncurried (Fn2 , runFn2 )
33+ import Data.Function.Uncurried (Fn4 , runFn4 )
3434import Data.HTTP.Method (Method (..), CustomMethod )
3535import Data.HTTP.Method as Method
3636import Data.List.NonEmpty as NEL
@@ -88,15 +88,21 @@ defaultRequest =
8888data Error
8989 = RequestContentError String
9090 | ResponseBodyError ForeignError (Response Foreign )
91- | XHRError Exn.Error
91+ | TimeoutError
92+ | RequestFailedError
93+ | XHROtherError Exn.Error
9294
9395printError :: Error -> String
9496printError = case _ of
9597 RequestContentError err ->
9698 " There was a problem with the request content: " <> err
9799 ResponseBodyError err _ ->
98100 " There was a problem with the response body: " <> renderForeignError err
99- XHRError err ->
101+ TimeoutError ->
102+ " There was a problem making the request: timeout"
103+ RequestFailedError ->
104+ " There was a problem making the request: request failed"
105+ XHROtherError err ->
100106 " There was a problem making the request: " <> Exn .message err
101107
102108-- | The type of records that represents a received HTTP response.
@@ -179,16 +185,19 @@ request req =
179185 Left err ->
180186 pure $ Left (RequestContentError err)
181187 where
182-
183188 send :: Nullable Foreign -> Aff (Either Error (Response a ))
184189 send content =
185- try (AC .fromEffectFnAff (runFn2 _ajax ResponseHeader (ajaxRequest content))) <#> case _ of
190+ try (AC .fromEffectFnAff (runFn4 _ajax timeoutErrorMessageIdent requestFailedMessageIdent ResponseHeader (ajaxRequest content))) <#> case _ of
186191 Right res ->
187192 case runExcept (fromResponse res.body) of
188193 Left err -> Left (ResponseBodyError (NEL .head err) res)
189194 Right body -> Right (res { body = body })
190195 Left err ->
191- Left (XHRError err)
196+ let message = Exn .message err
197+ in Left $
198+ if message == timeoutErrorMessageIdent then TimeoutError
199+ else if message == requestFailedMessageIdent then RequestFailedError
200+ else XHROtherError err
192201
193202 ajaxRequest :: Nullable Foreign -> AjaxRequest a
194203 ajaxRequest =
@@ -227,6 +236,12 @@ request req =
227236 addHeader (Accept <$> ResponseFormat .toMediaType req.responseFormat)
228237 req.headers
229238
239+ timeoutErrorMessageIdent :: String
240+ timeoutErrorMessageIdent = " AffjaxTimeoutErrorMessageIdent"
241+
242+ requestFailedMessageIdent :: String
243+ requestFailedMessageIdent = " AffjaxRequestFailedMessageIdent"
244+
230245 addHeader :: Maybe RequestHeader -> Array RequestHeader -> Array RequestHeader
231246 addHeader mh hs = case mh of
232247 Just h | not $ any (on eq RequestHeader .name h) hs -> hs `Arr.snoc` h
@@ -261,7 +276,9 @@ type AjaxRequest a =
261276
262277foreign import _ajax
263278 :: forall a
264- . Fn2
279+ . Fn4
280+ String
281+ String
265282 (String -> String -> ResponseHeader )
266283 (AjaxRequest a )
267284 (AC.EffectFnAff (Response Foreign ))
0 commit comments