Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Fix definition of success in newXMLHttpRequestWithError #415

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

TheKK
Copy link

@TheKK TheKK commented Apr 27, 2021

Here the sample code demonstrates this change:

example :: (MonadWidget t m) => m ()
example = mdo
  failureE <- button "failure"

  divClass "" $ do
    text "getAndDecode"
    simpleList e1Dyn $ el "li" . display

  divClass "" $ do
    text "performRequestAsync"
    simpleList e2Dyn $ el "li" . display

  divClass "" $ do
    text "performRequestAsyncWithError"
    simpleList e3Dyn $ el "li" . display

  e1E :: Event t (Maybe ()) <- getAndDecode
     $ "https://nnnnnnnnnnn"
    <$ failureE

  e2E :: Event t XhrResponse <- performRequestAsync
     $ xhrRequest "GET" "https://nnnnnnnnnnn" def
    <$ failureE

  e3E :: Event t (Either XhrException XhrResponse) <- performRequestAsyncWithError
     $ xhrRequest "GET" "https://nnnnnnnnnnn" def
    <$ failureE

  e1Dyn <- foldDyn (:) [] e1E
  e2Dyn <- foldDyn (:) [] $ fmap (_xhrResponse_statusText) e2E
  e3Dyn <- foldDyn (:) [] $ fmap (fmap _xhrResponse_status) e3E

  return ()

Before this change, press the button to cause Xhr error and you got:
a

After this change, press the button to cause Xhr error and you got:
b

It's great that performRequestAsyncWithError stops returning Right 0 on error and reports exception correctly. But now performRequestAsync and getAndDecode become silent, which would need some discussion.

In the case of performRequestAsync, it's appropriate to keep silence on error since it return Event t XhrResponse and we won't get one on the error case.

For getAndDecode which creates Event t (Maybe a), should we map Xhr error to Nothing or not emitting this event?

When ready state is DONE (4), we also needs to check its status to know if it
is success or not. According to MDN [1]:

  Before the request completes, the value of status is 0. Browsers also report a
  status of 0 in case of XMLHttpRequest errors.

[1]: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant