Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Detecting failures using isc.FileLoader.cacheFiles

    v8.3p_2012-11-21/PowerEdition Deployment

    I am using cacheFiles to load an image in the background. I have a callback that then displays it to the user once it has loaded.

    My problem is that if the image fails to load, I want to detect that and do some error handling. This got me digging:

    Code:
    ...
    _cacheImages:function(){
        var html="";
        while(this._imageQueue.length){
            var fileID=this._imageQueue.shift();
            var URL=this._fileConfig[fileID].URL;
            var callback="if(window.isc)isc.FileLoader.fileLoaded(\""+fileID+"\")";
            html+="<IMG SRC='"+URL+"' onload='"+callback+"' onerror='"+callback+"' onabort='"+callback
                 +(isc.Browser.isOpera?"' STYLE=visibility:hidden;position:absolute;top:-1000px'>"
                                        :"' STYLE='display:none'>");
        }
        this._insertHTML(html);
    },
    ...
    onload, onerror and onabort all are handled by the same callback. Looking further downstream in the code, it seems there is no way to detect which one of those conditions triggered the callback.

    fileLoaded currently takes four arguments:

    Code:
    ...
    fileLoaded:function(fileID,fileContents,ignoreThisArg,delayed){
    ...
    My solution was to add a fifth argument to fileLoaded called loadStatus.

    I changed _cacheImages to pass either "success", "error" or "abort" as the loadStatus when it calls fileLoaded.

    Finally, I made fileLoaded set a loadStatus property on the fileConfig and that's all I need.

    Code:
    _completeLoad:function(fileID){
        
        var fileConfig=this._fileConfig[fileID];
        this._checkISCInit();
        if(fileConfig.onload){
            if(this.isAString(fileConfig.onload))isc.evalSA(fileConfig.onload);
            else fileConfig.onload(fileConfig);
        }
    ...
    fileLoaded calls _completeLoad. Which in turn calls the onload callback from my original call to FileLoader.cacheFiles, passing it the fileConfig object.

    I now have the loadStatus available within the onload callback.

    Observations: I did it this way around because it involved the fewest changes to the obfuscated code, but I would love to see a proper Isomorphic solution for handling failed cacheFiles calls!

    #2
    There's nothing wrong with your patch code, but, cacheFiles() isn't really intended to have error handling; in the scenario where cacheFiles() is used, the images are just expected to be there.

    If you have a need to both check for the existence of a file and cache it if it's present, we'd probably recommend rolling your own code.

    Comment


      #3
      Originally posted by Isomorphic View Post
      ...cacheFiles() isn't really intended to have error handling; in the scenario where cacheFiles() is used, the images are just expected to be there...
      Given that I knew the images existed, that was my sentiment, too, until I had a 504 Gateway timeout error whilst retrieving an image. So it's not so much existence checking, it's error handling when there are transport problems.

      It'd be great if you would consider putting error handling in, otherwise we have to test that the obfuscated patch works with each upgrade we do.

      Comment

      Working...
      X