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);
}
|