Inconsistencies with Flash and HTTP status codes.
The RESTful service that my Flash app is consuming is one that is being custom built by a third party for my application. If a successful transaction takes place, I get an XML response with some useful data. If the transaction was unsuccessful, I get a predictable XML response containing an error code and message that are specific to the application. However, the back-end developer is using HTTP status codes in the range of 400 and greater for errors. The Flash documentation indicates that when using the URLLoader class, its data property will only be populated if the transaction completes successfully. Its HTTPStatusEvent event is considered to be unreliable due to inconsistencies across browsers. Therefore, I'm only really listening for Event.COMPLETE, IOErrorEvent.IO_ERROR and SecurityErrorEvent.SECURITY_ERROR.
Whenever I get an error response, (which for me could happen if a session is expired or if bad data is passed through...), I get valid XML back with a custom status code within the XML itself, and an HTTP status code of say, 405. A successful response would have an HTTP status code of 200. Event.COMPLETE is only triggered when the HTTP status code is 200, which means OK. Anything else triggers the IOErrorEvent.IO_ERROR event. What I was doing is putting try, catch around my attempt to access the error XML. This way I could catch if the response wasn't even valid XML and show some generic message. Then within the try block, I had a switch statement that would look for the custom status codes within my XML so that I could show more specific feedback to the user, like "Your session has timed out, click here to sign in again".
This all worked actually. I develop on OS X 10.6 and was using FireFox 3.6.x. For some reason though, my builds, when deployed to the dev server, were not working for some of my other team mates. We confirmed that it wasn't their cache, and that the deployment was intact and was indeed the latest build. They were also using FireFox but on Windows 7. Through the use of some packet sniffers we were able to confirm that in my environment I was getting past the first web service call and my colleague was not. I then tested again in OS X but with the latest version of Safari. In Safari I had the same failure as my colleague and was able to confirm that the data property of the URLRequest object was not being populated in my IO_ERROR handler (whereas for me, in FireFox it was).
The solution unfortunately must be that I had to get the back-end web service developer to modify his logic so that all application errors that would return error related XML for the application, be returned with an HTTP status code of 200. I have consumed XML from many RESTful services in the past and have never run into this issue in all my years. I believe I have just taken for granted that all these other services always return an HTTP status code of 200 when they want to return you useful XML error messages.
Hopefully this saves someone else some time!
Labels: as3 flash
