OpenF2 / F2

Redefining web integration for the financial services community
Apache License 2.0
130 stars 62 forks source link

Manifest load error handling fails for cross domain JSONP calls #112

Open ilinkuo opened 11 years ago

ilinkuo commented 11 years ago

I found this problem in 1.1.2 and checked that the handling was still the same in 1.2.

When the manifest fails to load, the app is not removed

Root cause: Lines 16523-16528 of https://github.com/OpenF2/F2/blob/master/sdk/f2.debug.js are not invoked

errorFunc = function() {
    jQuery.each(req.apps, function(idx,item){
        F2.log('Removed failed ' +item.name+ ' app', item);
        F2.removeApp(item.instanceId);
    });
},

This is a limitation of jQuery's ajax handler. See http://api.jquery.com/jQuery.ajax/

error Type: Function( jqXHR jqXHR, String textStatus, String errorThrown ) A function to be called if the request fails. The function receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred. Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror". When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error." As of jQuery 1.5, the error setting can accept an array of functions. Each function will be called in turn. Note: This handler is not called for cross-domain script and cross-domain JSONP requests. This is an Ajax Event.

Please make the behavior the same for all requests, regardless of domain.

Implementation suggestion: Put the removal check in the :complete handler and have the success set a flag to be checked.

ilinkuo commented 11 years ago

It turns out not only is the :error handler not called, the :complete handler is also not called, so my implementation suggestion above will not work.

My temporary local hack to this problem was to create an associated setTimeout which called the removeApp() and place the corresponding clearTimeout in the complete handler. The value of the timeout was set at an arbitrary 6000ms.

brianbaker commented 11 years ago

Looks like passing a timeout parameter into $.ajax will allow jQuery to call the appropriate handlers. http://jsfiddle.net/rekabnairb/EJJbv/

ilinkuo commented 11 years ago

That timeout seems good to me.... I've found that it is a little tricky and haven't quite figured out how to do this. The problem is that even if the timeout causes the error handler to be thrown and then the app is removed, if the request comes back after the timeout, the code in the request will still run. There has to be a way to prevent the slow-loading code from running at all.