Straight “out of the box” error handling functionality in the jQuery version of ajaxCFC is solid, but basic. The attribute useDefaultErrorHandler defaults to false, and when an error is thrown up to ajaxCFC, two things are done.
The error is caught and handled in the __initialize_AjaxCFC() function which, in turn fires the alertError() method. If the debug setting is enabled, then the error is logged by ajaxCFC, otherwise a Javascript alert is produced.
if (jQuery.AjaxCFCHelper.getDebug() == true) {
log.error('Type: #jsStringFormat(arguments.objError.type)#, Message: #jsStringFormat(arguments.objError.message)#, File: #jsStringFormat(arguments.objError.TagContext[1].TEMPLATE)#:#jsStringFormat(arguments.objError.TagContext[1].LINE)#');
} else {
alert('An error has occurred:\nMessage: #jsStringFormat(arguments.objError.message)#\nFile: #jsStringFormat(arguments.objError.TagContext[1].TEMPLATE)#:#jsStringFormat(arguments.objError.TagContext[1].LINE)#');
}
However, in my applications, I prefer the user not see generic, unintelligible error message in Javascript alerts, so I set useDefaultErrorHandler to false when making ajaxCFC calls. Then I can use the built-in success() and error() methods proviby the library. Here’s an example.
However, I was still seeing the Javascript alert produced by ajaxCFC, so I added another condition to the alertError() method code. It checks to see if the useDefaultErrorHandler property was enabled, and if so, bypasses throwing up the alert or logging the message.
if (jQuery.AjaxCFCHelper.useDefaultErrorHandler == true) {
if (jQuery.AjaxCFCHelper.getDebug() == true) {
log.error('Type: #jsStringFormat(arguments.objError.type)#, Message: #jsStringFormat(arguments.objError.message)#, File: #jsStringFormat(arguments.objError.TagContext[1].TEMPLATE)#:#jsStringFormat(arguments.objError.TagContext[1].LINE)#');
} else {
alert('An error has occurred:\nMessage: #jsStringFormat(arguments.objError.message)#\nFile: #jsStringFormat(arguments.objError.TagContext[1].TEMPLATE)#:#jsStringFormat(arguments.objError.TagContext[1].LINE)#');
}
}
Now I can handle any error with custom Javascript code inside the $.AjaxCFC() call, but what about keeping a history of errors? I always maintain a log of errors produced by my application, but I never use Javascript to do so, I use the
I needed a way to have built-in error handling accessible to any of my components that extended model.ajax.ajax. My solution was to add a method to the Ajax component that would accept two parameters – the name of the log file in which to log errors, and the cfcatch object. I could then log all of the attributes of the error with one simple call.
<cffunction name="logError" access="package" output="no" returntype="void">
<cfargument name="logFile" required="true" type="string">
<cfargument name="caughtError" required="true">
<cfloop collection="#arguments.caughtError#" item="errorKey">
<cfif isSimpleValue(arguments.caughtError[errorKey])>
<cflog file="#arguments.logFile#" type="error" text="#errorKey#: #arguments.caughtError[errorKey]#">
</cfif>
</cfloop>
<cfset throw(arguments.caughtError.type, arguments.caughtError.message, arguments.caughtError.detail, arguments.caughtError.errorCode, arguments.caughtError.extendedInfo)>
</cffunction>
One Response for "Enhancing ajaxCFC for Custom Error Handlers"
Been a while.
I have some cf ?s. Send me an email.
Kyle