Search code examples
actionscript-3flasheventsioerrorurlloader

Why don't IOErrorEvents contain the URL when running standalone?


Here is a simple example of an URLLoader.

var loader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest("http://example.com/doesntexist.txt");
loader.addEventListener(IOErrorEvent.IO_ERROR, function(e:IOErrorEvent){
    textbox.text = e.toString(); // Text box on stage
});
loader.load(request);

This behaves weirdly.

When running from Flash or debugging from Flash, the error looks like this.

[IOErrorEvent type="ioError" bubbles=false cancelable=false eventPhase=2 text="Error #2032: Stream Error. URL: http://example.com/doesntexist.txt"]

But, when running as a .swf or an .exe projector, it looks like this.

[IOErrorEvent type="ioError" bubbles=false cancelable=false eventPhase=2 text="Error #2032"]

Why is this so? Is there a way to get the first result when standalone?

EDIT: I need to get it working as a projector.


Solution

  • While I'm not sure about the why (probably an efficiency thing behind the scenes in flash player), here is a way to accomplish what you'd like:

    You can write a custom class that extends URLLoader that stores the url for you.

    package 
    {
        import flash.net.URLLoader;
        import flash.net.URLRequest;
    
        public class MyURLLoader extends URLLoader
        {
            public var request:URLRequest; //all were doing here is adding this public property and setting it when loading into the URLLoader
    
            public function MyURLLoader(request_:URLRequest) {
                request = request_;
                super(request);
            }
    
            override public function load(request_:URLRequest):void 
            {
                request = request_;
                super.load(request);
            }
        }
    }
    

    Then when handling your error, you can reference the URL like so:

    var loader:MyURLLoader = new MyURLLoader();
    var request:URLRequest = new URLRequest("http://example.com/doesntexist.txt");
    
    loader.addEventListener(IOErrorEvent.IO_ERROR, function(e:IOErrorEvent){
        textbox.text = e.toString() + " URL: " + MyURLLoader(e.currentTarget).request; // Text box on stage
    });
    loader.load(request);