The following sections briefly describe how you can handle errors in Application.cfc. For more information on error pages and error handling, see Handling Errors For details on implementing the onError method, see onError in the CFML Reference.
Application.cfc error handling techniques
Application.cfc can handle errors in any combination of the following ways:
These techniques let you include application-specific information, such as contact information or application or version identifiers, in the error message, and let you display all error messages in the application in a consistent manner. You can use Application.cfc to develop sophisticated application-wide error-handling techniques, including error-handling methods that provide specific messages, or use structured error-handling techniques.
Handling server-side validation errors in the onError method
Server-side validation errors are actually ColdFusion exceptions; as a result, if your application uses an onError method, this method gets the error and must handle it or pass it on to ColdFusion for handling.
To identify a server-side validation error, search the Arguments.Exception.StackTrace field for coldfusion.filter.FormValidationException. You can then handle the error directly in your onError routine, or throw the error so that either the ColdFusion default validation error page or a page specified by an cferror tag in your Application.cfc initialization code handles it.
Example: error Handling with the onError method
The following Application.cfc file has an onError method that handles errors as follows:
<cfcomponent> <cfset This.name = "BugTestApplication"> <cffunction name="onError"> <!--- The onError method gets two arguments: An exception structure, which is identical to a cfcatch variable. The name of the Application.cfc method, if any, in which the error happened. <cfargument name="Except" required=true/> <cfargument type="String" name = "EventName" required=true/> <!--- Log all errors in an application-specific log file. ---> <cflog file="#This.Name#" type="error" text="Event Name: #Eventname#" > <cflog file="#This.Name#" type="error" text="Message: #except.message#"> <!--- Throw validation errors to ColdFusion for handling. ---> <cfif Find("coldfusion.filter.FormValidationException", Arguments.Except.StackTrace)> <cfthrow object="#except#"> <cfelse> <!--- You can replace this cfoutput tag with application-specific error-handling code. ---> <cfoutput> <p>Error Event: #EventName#</p> <p>Error details:<br> <cfdump var=#except#></p> </cfoutput> </cfif> </cffunction> </cfcomponent>
To test this example, put a CFML page with the following code in the same page as the Application.cfc file, and enter valid and invalid text in the text input field.
<cfform> This box does Integer validation: <cfinput name="intinput" type="Text" validate="integer" validateat="onServer"><br> Check this box to throw an error on the action page: <cfinput type="Checkbox" name="throwerror"><br> <cfinput type="submit" name="submitit"> </cfform> <cfif IsDefined("form.fieldnames")> <cfif IsDefined("form.throwerror")> <cfthrow type="ThrownError" message="This error was thrown from the bugTest action page."> <cfelseif form.intinput NEQ ""> <h3>You entered the following valid data in the field</h3> <cfoutput>#form.intinput#</cfoutput> </cfif> </cfif>