Debugging ajax calls with ColdFusion part deux


A few days ago I wrote a post about debugging ajax calls with ColdFusion.  My approach was to basically write every remote method within a cftry/cfcatch block.  Ray suggested I use the new onCFCRequest() method built in to CF9, unfortunately I am not running CF9.

However it did get me to thinking about ways I could create a wrapper for handling errors in my remote methods.  I tried several different ways based on onRequestStart and onRequest, but nothing really came of it.  Next I tried setting up my error handling strangely enough in the onError method.  This method works, but I am still not quite sure how I feel about it.  Here is what I worked out:

<cffunction name="onError" access="public" returntype="void" output="true">
 <cfargument name="Exception" type="any" required="true"/>
 <cfargument name="EventName" type="string" required="false" default="" />
 <cfset var result = structNew()/>
 <cfif arrayLen(arguments.exception.tagContext) and lcase(listLast(listLast(arguments.exception.tagContext[1].template, '/'), '.')) EQ "cfc">
  <cfset result['svrStatus'] = "-1"/>
  <cfset result['svrMessage'] = arguments.exception.message/>
  <cfoutput>#serializeJSON(result)#</cfoutput>
 <cfelse>
  <cfdump var="#arguments#"/>
 </cfif>
</cffunction>

This actually works out fairly well. When fired, the onError method checks to see if it was called from a CFC or a CFM page.  If it was called from a CFC it sets up the default return values for an error and then returns the data as a JSON object.  What I do not like about this method is, every CFC returns the dump as a JSON object, whether it is a remote remote method or not.  I would rather have a hard error if an application-scoped CFC failed, but I guess we cannot have it all.  I would have to say it makes a lot more sense to handle things this way instead of writing every CFC method in a try/catch block.

Advertisements

About Robert Zehnder

Web application developer specializing in ColdFusion/Railo and Open Source development.

Posted on March 22, 2010, in ColdFusion, Railo. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: