View Full Version : php error handling in flex
how do i make an error in php (back end) look like an error in flex when i send data from php to flex? apart from creating an xml error tag n then identifying the tag in flex... is there any other way?
Sly_cardinal
07-07-2008, 11:34 AM
I'm assuming that you are using the Flex remote service classes.
When you make a service call you add a listener to the service that implements the IResponder (http://livedocs.adobe.com/flex/2/langref/mx/rpc/IResponder.html) interface.
The PHP Exception class constructor takes two parameters - a message and a fault code. The FaultEvent (http://livedocs.adobe.com/flex/2/langref/mx/rpc/events/FaultEvent.html) passed to your IResponder fault handler contains a fault property (http://livedocs.adobe.com/flex/2/langref/mx/rpc/Fault.html) that contains the details from the PHP exception.
public function getProducts($postcode)
{
$pdo = $this->getDatabaseConnection();
$productList = array();
// Do SQL query here...
$rowCount = $statement->rowCount();
if ($rowCount <= 0)
{
throw new Exception(ExceptionHandler::$INVALID_POSTCODE_ERRO R, ExceptionHandler::$INVALID_POSTCODE_ERROR_CODE);
}
else
{
// Populate productList here...
}
return $productList;
}
You can use the 'faultCode' and 'message' properties of this fault object to work out how to handle the error in Flex.
public function fault(event:Object):void
{
IEventDispatcher(event.target).removeEventListener (ResultEvent.RESULT, this.result);
IEventDispatcher(event.target).removeEventListener (FaultEvent.FAULT, this.fault);
var errorCode:int = event.fault.errorCode;
var faultString:int = event.fault.faultString;
// Display an error to the user.
new Alert("Error", faultString);
// Or, throw an error, giving details to the user:
throw new Error("PHP Error...");
}
In my project we have an error handling class that uses the errorCode to determine the error message to display to the user.
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.