Avg. Rating 5.0

Problem

While working on a Flex 4 project with a ColdFusion Remoting connection, I lamented over Twitter that it would be great if I could handle data errors in my CFC that would fire fault handlers in my Flex project. Kevin Schmidt contacted me to let me know that this was indeed possible.

Solution

Using the <cfthrow> tag in your ColdFusion Component allows you to create and use custom errors in Flex.

Detailed explanation

ASSUMPTIONS: I am assuming that you are:

  • - using Flash Builder 4
  • - using ColdFusion 9
  • - a Flex developer who knows how to set up Data Services and Data Centric Development wizards in Flash Builder 4
  • - a ColdFusion developer who knows how to set up a CFC

 

In this set of demonstration code, I have created a scenario in which an ArrayCollection is being sent to the server. This use case is that to prevent mis-handling of data (the server is expecting data in the Array), the received Array is tested for length. If there is no data, then something is wrong, thus the error is getting thrown.

[For more information about using ColdFusion's cfthrow tag, please see Kevin Schmidt's ColdFusion Cookbook entry as well as the ColdFusion documentation.]

Using ColdFusion Builder I've created a method in my CFC that will throw the error:

<cffunction name="getSomeArray" access="remote" returntype="array">
        <cfargument name="inputArray" type="array" required="true">
       
        <cfif ArrayLen(arguments.inputArray) LT 1>
            <cfthrow type="FaultEvent"
                    message="The server method getSomeArray did not receive proper data."
                    errorcode="SERVER1000"
                    detail="The array sent to method getSomeArray was empty: Length = #ArrayLen(arguments.inputArray)#" />
        <cfelse>
            <cfreturn inputArray />
        </cfif>
       
    </cffunction>

Inside the cfthrow tag I am using the message, errorcode and detail attributes to send information to my fault handler in Flex.

In the Flex demonstration code, I have created the standard service and handlers letting Flash Builder and the Data Centric Developement features to do a lot of the code writing for me. The fault handler for my CallResponder looks like this:

protected function getSomeArrayCallResponder_faultHandler(event:FaultEvent):void {

     trace("fault");
     var fault:Fault = event.fault;

     // STRIP OUT THE "Unable to invoke CFC - " THAT APPEARS IN FRONT OF OUR ERROR INFO
     var faultString:String = fault.faultString.split(" - ")[0];
               
     /*
          CONDITIONAL CODE HERE:
          THIS IS THE AREA WHERE YOU MIGHT PLACE CODE FOR HANDLING DIFFERENT ERRORS THAT MAY OCCUR
          FOR NOW, WE'RE JUST GOING TO SHOW AN ERROR THAT IS DEVELOPER-FRIENDLY
      */
      Alert.show(fault.faultDetail + "\n\nFault Code: " + fault.faultCode, faultString);
}

This method catches the error thrown from our ColdFusion component and parses the data we have placed inside the error event into an Alert box.


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes