Recipe 21.3. Handling Web Services Errors


Problem

You want to handle errors from a web service.

Solution

Listen for a fault event.

Discussion

When a web services error occurs, the operation dispatches a fault event of type mx.rpc.events.FaultEvent. You can add a listener to the Operation object directly. However, it's generally advisable to handle fault events at the WebService object level. Operation fault events bubble up to the WebService object if they aren't handled at the Operation level. The following adds a fault event listener to a WebService object:

webService.addEventListener(FaultEvent.FAULT, onWebServiceFault);

The FaultEvent class defines a fault property of type mx.rpc.Fault. Fault objects return details about the error using the faultCode, faultDetail, faultString, and rootCause properties. The following displays an Alert when an error occurs:

private onWebServiceFault(event:FaultEvent):void {     var fault:Fault = FaultEvent.fault;     var message:String = "An error occurred. The details are as follows\ncode: " + fault.faultCode;     message += "\ndetail: " + faul.faultDetail;     Alert.show("Web Service Error", message); }




ActionScript 3. 0 Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2007
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net