oracle / cordova-plugin-wkwebview-file-xhr

Cordova Plugin for WebView File XHR
Universal Permissive License v1.0
138 stars 120 forks source link

XMLHttpRequest.onerror is called erroneously if status code >= 400 #9

Closed vbraun closed 6 years ago

vbraun commented 6 years ago

The onerror callback should only be called on network-level errors; If there is a response then, by definition, there was no network level error. The value of the status code is irrelevant. More discussion is at https://stackoverflow.com/questions/10584318/when-should-xmlhttprequests-onerror-handler-fire

if (payload.response.statusCode === 0 || payload.response.statusCode >= 400)
  reqContext.dispatchProgressEvent("error", respSize);

For the record, angularjs registers an onerror handler that correctly discards any results. So all $http calls that return a statusCode >= 400 now look like network errors.

gvanmat commented 6 years ago

It's simulating the same behavior for a native JS XMLHttpRequest. If you went after a resource that was not found or a 500 server except, the XMLHttpRequest fires a "error" event.

vbraun commented 6 years ago

Well a 404 doesn't, for example open the console on this page and run:

var xmlhttp = new XMLHttpRequest(),
  method = 'GET',
  url = 'https://github.com/foo/bar';

xmlhttp.open(method, url, true);
xmlhttp.onerror = function () {
  console.log("** An error occurred during the transaction");
};
xmlhttp.send();

does NOT call onerror.

A CORS error will trigger onerror though, i.e. if you run the same snippet from the console on a different website then it will be called.

gvanmat commented 6 years ago

I see what you mean now. My testbed was a standalone html page loaded from the local file system which would simulate CORS restrictions. Thanks for reporting the issue.

gvanmat commented 6 years ago

I just pushed in a fix. Please uninstall and re-install the plugin from the master to verify the fix:

cordova plugin remove cordova-plugin-wkwebview-file-xhr cordova plugin add https://github.com/oracle/cordova-plugin-wkwebview-file-xhr.git