Tech Blog

Creating reliable webservices with ColdFusion and Flex

Posted At : October 28, 2006 8:23 PM

The biggest difficulty with developing web services in CFMX is that when things go wrong it can be very difficult to figure out what has actually happened - there is no debug output on the bottom of the page. There is no big display of what has gone wrong as there is no page to put this on.

Also when errors occur and cf is configured to set HTTP status codes many errors will return a 500 status code and much of the information will not get passed from the browser to the flex client.

One way to see what is going on is to redirect everything through the java sniffer application that comes with cfmx

[cfinstall dir]\runtime\bin\sniffer.exe
or use something like wireshark to view all the network traffic.

However - there are many places where this does not work - for example if your application is working perfectly on your development machine but not when you push it live (or to staging - we're all using staging servers right!)

In this situation you may not be able to view the traffic so you end up searching all the logs looking for errors.

In order to prevent the 500 errors from occuring I have now taken to adding a status field and a message field to the return values of all webservice methods. There is then a try catch around the whole method which passes back a reasonably useful error message if something goes wrong.

<cffunction name="authenticate" returntype="struct" output="false" access="remote">
   <cfargument name="username" type="string" required="true">
   <cfargument name="password" type="string" required="true">
   <cfset var stAuthDetails = "">
   <cftry>
      <!--- Put authenticate code here that creates stAuthDetails struct--->
      <cfcatch type="any">
         <cfscript>
            stAuthDetails = structNew();
            stAuthDetails['id'] = 0;
            stAuthDetails['roles'] = '';
            stAuthDetails['status'] = false;
            stAuthDetails['message'] = '#cfcatch.message#';
         </cfscript>
      </cfcatch>
   </cftry>
   <cfreturn stAuthDetails>
</cffunction>

The status field will return true if it is successful and false if something went wrong. Your application can then display the message somewhere in your application so that you have a clue what has gone wrong.