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

[XMLHttpRequest] Add support for ontimeout and onerror handler when using XMLHttpRequest for Android and iOS #6841

Conversation

grgmo
Copy link
Contributor

@grgmo grgmo commented Apr 6, 2016

Currently React-Native does not have ontimeout and onerror 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 and onerror 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 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.onerror = onError;
request.open('GET', 'https://mywebsite.com/endpoint.php');
request.send();

@facebook-github-bot
Copy link
Contributor

By analyzing the blame information on this pull request, we identified @lexs, @nicklockwood and @mkonicek to be potential reviewers.

@facebook-github-bot facebook-github-bot added GH Review: review-needed CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. labels Apr 6, 2016
@mkonicek
Copy link
Contributor

mkonicek commented Apr 7, 2016

I'll let @lexs review this one.

@mkonicek
Copy link
Contributor

mkonicek commented Apr 7, 2016

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")) {
Copy link
Contributor

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.

@grgmo grgmo force-pushed the xmlhttprequest-add-missing-ontimeout-onerror-handlers branch from 2c85103 to c58e929 Compare April 9, 2016 18:09
@facebook-github-bot
Copy link
Contributor

@grgmo updated the pull request.

@grgmo grgmo force-pushed the xmlhttprequest-add-missing-ontimeout-onerror-handlers branch from c58e929 to 7873aa9 Compare April 9, 2016 18:11
@facebook-github-bot
Copy link
Contributor

@grgmo updated the pull request.

@grgmo grgmo force-pushed the xmlhttprequest-add-missing-ontimeout-onerror-handlers branch from 7873aa9 to 3fae0bc Compare April 9, 2016 18:13
@facebook-github-bot
Copy link
Contributor

@grgmo updated the pull request.


xhr: XMLHttpRequest;

constructor(props) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parameter props Missing annotation

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parameter props Missing annotation

@grgmo grgmo force-pushed the xmlhttprequest-add-missing-ontimeout-onerror-handlers branch from 3fae0bc to ab924b8 Compare April 9, 2016 19:06
@facebook-github-bot
Copy link
Contributor

@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);
}
}

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.

@grgmo grgmo force-pushed the xmlhttprequest-add-missing-ontimeout-onerror-handlers branch from ab924b8 to 655e647 Compare April 9, 2016 20:55
@facebook-github-bot
Copy link
Contributor

@grgmo updated the pull request.

@grgmo grgmo force-pushed the xmlhttprequest-add-missing-ontimeout-onerror-handlers branch from 655e647 to 3f3c8f5 Compare April 10, 2016 14:16
@facebook-github-bot
Copy link
Contributor

@grgmo updated the pull request.

xhr._didCompleteResponse(1, 'Generic error');

expect(xhr.onerror).toBeCalledWith(null);
expect(xhr.ontimeout).not.toBeCalled();

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.

@grgmo grgmo force-pushed the xmlhttprequest-add-missing-ontimeout-onerror-handlers branch from 3f3c8f5 to 64940ee Compare April 10, 2016 14:56
@facebook-github-bot
Copy link
Contributor

@grgmo updated the pull request.

@grgmo
Copy link
Contributor Author

grgmo commented Apr 10, 2016

@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.

@lexs
Copy link
Contributor

lexs commented Apr 14, 2016

@facebook-github-bot shipit

@lexs
Copy link
Contributor

lexs commented Apr 14, 2016

Thanks! Looks great, sorry for taking a few days coming back to this.

@facebook-github-bot
Copy link
Contributor

Thanks for importing. If you are an FB employee go to Phabricator to review.

@lexs
Copy link
Contributor

lexs commented Apr 14, 2016

@grgmo Seems like there's merge issue, could you rebase and I'll reimport?

@grgmo
Copy link
Contributor Author

grgmo commented Apr 14, 2016

@lexs on it, will update the doc too. thanks for reviewing

@skellock
Copy link
Contributor

Thank you patchers and mergers. <3

@skellock
Copy link
Contributor

@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:

<= iOS 8.2 The operation couldn’t be completed. (NSURLErrorDomain error -1001.)
>= iOS 8.3 The request timed out.
android 22 - SSL handshake timed out (if a low timeout is set and server is ssl)
android 22 - timeout (if a high timeout is set)
android 16 - Read timed out (if a high timeout is set)

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)

@lexs
Copy link
Contributor

lexs commented Apr 14, 2016

@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 ontimeout and the like.

@skellock
Copy link
Contributor

Well this PR is a beautiful first step. Now that I know the difference between timeout and other, that's good enough to relay better info on to the user. Thank you.

@grgmo grgmo force-pushed the xmlhttprequest-add-missing-ontimeout-onerror-handlers branch from 64940ee to 6332a4a Compare April 14, 2016 12:46
@facebook-github-bot
Copy link
Contributor

@grgmo updated the pull request.

@grgmo grgmo force-pushed the xmlhttprequest-add-missing-ontimeout-onerror-handlers branch from 6332a4a to 235c24e Compare April 14, 2016 17:00
@facebook-github-bot
Copy link
Contributor

@grgmo updated the pull request.

@grgmo
Copy link
Contributor Author

grgmo commented Apr 15, 2016

@lexs rebased

@lexs
Copy link
Contributor

lexs commented Apr 15, 2016

@facebook-github-bot shipit

@facebook-github-bot
Copy link
Contributor

Thanks for importing. If you are an FB employee go to Phabricator to review.

@ghost ghost closed this in d09cd62 Apr 15, 2016
zebulgar pushed a commit to nightingale/react-native that referenced this pull request Jun 18, 2016
…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
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants