4 Replies Latest reply on Sep 1, 2009 9:51 PM by dandante

    catching policy file errors in try/catch

    dandante

      Hi,

       

      I have a socket policy server that works just fine.

       

      If however the page is loaded and the socket policy server isn't running, I want to be able to catch the error.

      Here is an example:

       

       

      try{

           Security.loadPolicyFile("xmlsocket://127.0.0.1:9876");

           stomp.connect("127.0.0.1", 61613, ch);//causes an attempt at socket communication

      } catch (e) {

           Alert.show("a real error","title");

      } finally {

           trace("finally....");

      }

      When this runs, I never see the Alert. I do see the "finally...." message in my console., and then the actual error messages:
      Error: Failed to load policy file from xmlsocket://127.0.0.1:9876
      Error: Request for resource at xmlsocket://127.0.0.1:61613 by requestor from xxxx has failed because the server cannot be reached.
      *** Security Sandbox Violation ***
      Connection to 127.0.0.1:61613 halted - not permitted from xxxxx
      Error #2044: Unhandled securityError:. text=Error #2048: Security sandbox violation: xxx cannot load data from 127.0.0.1:61613.
              at org.codehaus.stomp::Stomp()[C:\Users\wischusen\Documents\FlexBuilderProjects\Stomp\org\co dehaus\stomp\Stomp.as:52]
      I just want to catch the error and prompt the user to turn on their socket server. How do I do that?

        • 1. Re: catching policy file errors in try/catch
          babo_ya Level 3

          You can't show the Alert on the "finally"?

           

          BaBo,

          • 2. Re: catching policy file errors in try/catch
            dandante Level 1

            Yes, I can show the alert on the "finally". That does not accomplish anything, though, because the finally executes whether an exception was caught or not.

            • 3. Re: catching policy file errors in try/catch
              babo_ya Level 3

              I have a better idea..

               

              Instead of putting the try/catch on the loadPolicy function.. I'm sure you have socket object and you can add ioError or Securtiy error events....

              and If the security event is dispatched then most likely it's the socket server being down.

               

              hope this helps,

               

              BaBo,

              1 person found this helpful
              • 4. Re: catching policy file errors in try/catch
                dandante Level 1

                Okay, that sort of worked.

                 

                It's not an ideal solution because I am using a third party library (STOMP) and I don't want to edit that library. I don't have control over the private socket that it uses internally.

                 

                So what I did was just create a socket solely for the purpose of trying to catch this error.

                 

                Unfortunately I have to wait 22 seconds for the alert to pop up. This might have something to do with the timeout referenced here:

                http://livedocs.adobe.com/flex/3/langref/flash/system/Security.html#loadPolicyFile()

                 

                But that's only supposed to be three seconds.

                 

                It's weird because the player knows right away that the socket server is down--it tells me so in the log (actually--it does so on my work machine but not at home--perhaps due to different versions of the debug player). But I can't hook into that apparently.

                 

                Here is what I am doing, BTW:

                 

                Security.loadPolicyFile("xmlsocket://127.0.0.1:9876");

                var sock:Socket = new Socket();

                sock.addEventListener(IOErrorEvent.NETWORK_ERROR, errorHandler);

                sock.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);

                sock.addEventListener(ErrorEvent.ERROR, errorHandler);

                sock.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler);

                sock.connect("127.0.0.1", 61613);


                private function errorHandler(event:ErrorEvent):void {

                     trace("in error handler: " + event.text)

                     Alert.show("in error handler: " + event.text, "title");

                }

                 

                Output of the trace is:

                 

                in error handler: Error #2048: Security sandbox violation: http://xxx cannot load data from 127.0.0.1:61613.

                 

                Now I would like to know why I have to wait 22 seconds for that message. Is there some other event I can listen for that might fire sooner?

                 

                By the way, you can try this code yourself. It does not require that a socket server be running--in fact, you won't see the alert if there is a socket server running.

                 

                Thanks for your help.

                 

                Dan