-
Notifications
You must be signed in to change notification settings - Fork 24.4k
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
[XMLHttpRequest] Add support for ontimeout and onerror handler when using XMLHttpRequest for Android and iOS #6841
Conversation
By analyzing the blame information on this pull request, we identified @lexs, @nicklockwood and @mkonicek to be potential reviewers. |
I'll let @lexs review this one. |
Do we have an XHR example in the UI Explorer? Can you add this there? Can you add some unit / integration tests? |
@@ -326,6 +326,9 @@ private void onRequestError(ExecutorToken ExecutorToken, int requestId, String e | |||
WritableArray args = Arguments.createArray(); | |||
args.pushInt(requestId); | |||
args.pushString(error); | |||
if (error.equals("timeout")) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you make this instead of using the string perhaps pass down the IOException? I'm not sure exactly what the exception type it'll be but perhaps SocketTimeoutException
. Then just compare the getClass()
. This avoids depending on an implementation detail somewhere setting "timeout" as the message.
It's probably easiest to split onRequestError()
into two methods, one taking an error string and one taking an exception.
2c85103
to
c58e929
Compare
@grgmo updated the pull request. |
c58e929
to
7873aa9
Compare
@grgmo updated the pull request. |
7873aa9
to
3fae0bc
Compare
@grgmo updated the pull request. |
|
||
xhr: XMLHttpRequest; | ||
|
||
constructor(props) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
parameter props
Missing annotation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
parameter props
Missing annotation
3fae0bc
to
ab924b8
Compare
@grgmo updated the pull request. |
// We should send an event to handler, but since we don't process that | ||
// event anywhere, let's leave it empty | ||
onload(null); | ||
newEvent(null); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
eol-last: Newline required at end of file but not found.
ab924b8
to
655e647
Compare
@grgmo updated the pull request. |
655e647
to
3f3c8f5
Compare
@grgmo updated the pull request. |
xhr._didCompleteResponse(1, 'Generic error'); | ||
|
||
expect(xhr.onerror).toBeCalledWith(null); | ||
expect(xhr.ontimeout).not.toBeCalled(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no-mixed-spaces-and-tabs: Mixed spaces and tabs.
3f3c8f5
to
64940ee
Compare
@grgmo updated the pull request. |
@lexs i've made the changes, added unit test and example on UIExplorer. Comments from eslint-bot are also fixed. I need to update the Network documentation if you're ok with the changes. |
@facebook-github-bot shipit |
Thanks! Looks great, sorry for taking a few days coming back to this. |
Thanks for importing. If you are an FB employee go to Phabricator to review. |
@grgmo Seems like there's merge issue, could you rebase and I'll reimport? |
@lexs on it, will update the doc too. thanks for reviewing |
Thank you patchers and mergers. <3 |
@grgmo i know this PR is wrapping up, and the following question changes nothing, but i'm curious: does onerror bubble up some semblance of a error code? I ask because in my testing, i've seen the error messages appear differently on different devices: For example:
Not just timeouts too. Also non-http server errors. Seems like iOS 8.2 and under at least manifested the underlying error code, but that stopped at 8.3. And Android... well... TL;DR: Any chance we could harvest the underlying error codes? I wouldn't mind tackling that work in native side, but it feels a bit weird to decorate XMLHttpRequest with extra things that aren't in the spec. (potentially over/underthinking it). [edit 1: had to wrap my shrugger in backticks cuz he lost his right forearm) |
@skellock Yeah, problem with network errors is that there is no real standard way of expressing them as with HTTP error codes (404 etc). We could try to consolidate them (by exception type?) but as you said the XMLHttpRequest API isn't very granular and apart from |
Well this PR is a beautiful first step. Now that I know the difference between |
64940ee
to
6332a4a
Compare
@grgmo updated the pull request. |
6332a4a
to
235c24e
Compare
@grgmo updated the pull request. |
@lexs rebased |
@facebook-github-bot shipit |
Thanks for importing. If you are an FB employee go to Phabricator to review. |
d09cd62
…st for Android and iOS Summary:Currently React-Native does not have `ontimeout` and `onerror` handlers for [XMLHttpRequest](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest). This is an extension to [No timeout on XMLHttpRequest](facebook#4648). With addition to two handlers, both Android and iOS can now handle `ontimeout` if request times out and `onerror` when there is general network error. **Test plan** Code has been tested on both Android and iOS with [Charles](https://www.charlesproxy.com/) by setting a breakpoint on the request which fires `ontimeout` when the request waits beyond `timeout` time and `onerror` when there is network error. **Usage** JavaScript - ``` var request = new XMLHttpRequest(); function onLoad() { console.log(request.status); }; function onTimeout() { console.log('Timeout'); }; function onError() { console.log('General network error'); }; request.onload = onLoad; request.ontimeout = onTimeout; request.onerr Closes facebook#6841 Differential Revision: D3178859 Pulled By: lexs fb-gh-sync-id: 30674570653e92ab5f7e74bd925dd5640fc862b6 fbshipit-source-id: 30674570653e92ab5f7e74bd925dd5640fc862b6
Currently React-Native does not have
ontimeout
andonerror
handlers for XMLHttpRequest. This is an extension to No timeout on XMLHttpRequest.With addition to two handlers, both Android and iOS can now handle
ontimeout
if request times out andonerror
when there is general network error.Test plan
Code has been tested on both Android and iOS with Charles by setting a breakpoint on the request which fires
ontimeout
when the request waits beyondtimeout
time andonerror
when there is network error.Usage
JavaScript -