Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get HTTP status code in HTTPService fault handler

I am calling a server method through HTTPService from client side. The server is a RestFul web service and it might respond with one of many HTTP error codes (say, 400 for one error, 404 for another and 409 for yet another). I have been trying to find out the way to determine what was the exact error code sent by the server. I have walked teh entire object tree for the FaultEvent populated in my fault handler, but no where does it tell me the error code. Is this missing functionality in Flex?

My code looks like this: The HTTP Service declaration:

    <mx:HTTPService id="myServerCall" url="myService" method="GET" 
resultFormat="e4x" result="myServerCallCallBack(event)" fault="faultHandler(event)">
            <mx:request>
                <action>myServerCall</action>
                <docId>{m_sDocId}</docId>
            </mx:request>
        </mx:HTTPService>

My fault handler code is like so:

private function faultHandler(event : FaultEvent):void
{
 Alert.show(event.statusCode.toString() + " / " + event.fault.message.toString()); 
}
like image 673
Monty Avatar asked Jan 23 '23 07:01

Monty


1 Answers

I might be missing something here, but:

event.statusCode

gives me the status code of the HTTP response.

So I can successfully do something like this in my fault handler function:

public function handleFault(faultEvent:FaultEvent):void
{
    if (faultEvent.statusCode == 401)
    {
        Alert.show("Your session is no longer valid.", "", Alert.OK, this, loginFunc);
    }
    else
    {
        Alert.show("Failed with error code: " + faultEvent.statusCode as String);
    }
}
like image 164
Krustoff Avatar answered Jan 28 '23 09:01

Krustoff