Portal Home > Knowledgebase > Coldfusion > Example for ColdFusion 8/9 error handling within application.cfc


Example for ColdFusion 8/9 error handling within application.cfc




Here is an example for handling errors within your application.cfc file in ColdFusion 8:

<cfcomponent>
<cfset This.name = "ErrorHanldingExample">
<cfset This.LogPath = expandPath("CFError.log")>
<cfset This.ErrorTemplate = "MyErrorPage.cfm">

<!--- Handle 404 errors --->
<cffunction name="onMissingTemplate" returnType="boolean">
    <cfargument type="string" name="targetPage" required=true/>

<cftry>
   
    <!--- set response to 404 for Search Engine and statistical purposes --->
    <cfheader 
       statusCode = "404"
       statusText = "Page Not Found"
    >
   
    <!--- retrieve the error template from the application scope --->
    <cflock timeout="5" scope="application">
        <cfset ErrorTemplate = This.ErrorTemplate>
    </cflock>
    
    <!--- include a template to show to the user --->   
    <cfinclude template = "#ErrorTemplate#">
   
    <!--- return true to prevent the default ColdFusion error handler from running --->
    <cfreturn true />
   
    <cfcatch>
    <!--- If an error occurs within the error handler routine, allow ColdFusion's default error handler to run --->
        <cfreturn false />
    </cfcatch>
</cftry>

</cffunction>

<!--- Handle script related / 500 errors --->
<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/>
	
	<!--- Throw validation errors to ColdFusion for handling. --->
    <cfif Find("coldfusion.filter.FormValidationException", Arguments.Except.StackTrace)>
        <cfthrow object="#except#">
	<cfelse>
	    <cftry>
            <!--- Set response to 500 for Search Engine and statistics purposes --->
            <cfheader 
               statusCode = "500"
               statusText = "Internal Server Error"
            >
	    <!--- Log all errors in an application-specific log file. --->
		<cfset NewLine = chr(13) & chr(10)>
		<cfset ErrorMessage = "Event: #Eventname##NewLine#Error: #Except.Message##NewLine#">
                
                <!--- retrieve the log path from the application scope --->
                <cflock timeout="5" scope="application">
                    <cfset logPath = This.LogPath>
		</cflock>

		<cffile
		   action = "append"
		   file = "#logPath#"
		   output = "#ErrorMessage#"
		>
		
    		<!--- Run any application specific error handling code and include a template to be shown to the user --->

                <!--- retrieve the error template from the application scope --->
                <cflock timeout="5" scope="application">
                    <cfset ErrorTemplate = This.ErrorTemplate>
                </cflock>
    
                <!--- include a template to show to the user --->   
                <cfinclude template = "#ErrorTemplate#">
        
	    <!--- If an error occurs within the error handler, the cfcatch routine will run.  In that case, we will return a custom error to the user.  This should only happen if there is a permissions problem writing to the log or the error template does not exist. --->
	    <cfcatch>
	        <cfthrow message="An error has occured.  Additionally an error was encountered when processing the site's error handling routine. <br><br> #cfcatch.message#">
	    </cfcatch>
        </cftry>
    </cfif>
	
</cffunction>
</cfcomponent>


ColdFusion 8/9 provides handling of 404 errors and 500 errors through two separate functions in the application.cfc. The 'onMissingTemplate' function is used to handle 404 errors and the 'onError' function is used to handle 500 errors.

In the above example, there are two application variables that determine in what file 500 errors are logged and which page to display to the user when an error occurs:

 

<cfset This.LogPath = expandPath("CFError.log")>
<cfset This.ErrorTemplate = "MyErrorPage.cfm">

This is just an example to show those who are new to error handling in ColdFusion 8 how these functions can be used.

NOTE: If your site uses an 'application.cfm' file, it would need to be converted to an 'application.cfc' file(requires code changes) in order to utilize these functions. If both an 'application.cfc' and an 'application.cfm' are present in the same folder of your ColdFusion application, only the 'application.cfc' will run and the 'application.cfm' will be ignored.



Was this answer helpful?

Add to Favourites Add to Favourites    Print this Article Print this Article

Also Read
Do you support RDS? (Views: 1567)

* ASP is only available on Windows

Home Page
About
Web Hosting
FAQ
Get Support
Script Downloads

Coldfusion Hosting
ASP Hosting
Linux Hosting
Railo Hosting

Terms of Service
Privacy Policy
Affiliate Program

* ASP is only available on Windows.