Error Handling in Coldfusion
An exception is when something unexpected occurs, resulting in a nasty looking error message. You can use error handling techniques to handle such situations and display a more user friendly error message to the user. ColdFusion provides the following error handling tools to assist developers in catching errors. cftry/cfcatch:cftry/cfcatch is used for programmatic handling of errors. onError():
errors that are not handled by cftry/cfcatch or cfthrow/cfrethrow errors are handled in the onError() of Application.cfc, if present. <cferror >
if onError() method is not implemented then you can introduce the 3rd error handling layer using CFERROR tags in the coldfusion pages.
If you are using Application.cfc the best palce to use CFERROR tags is in the onRequestStart() method.
If you are using Application.cfm, put CFERROR tags in the Application.cfm file itself. cfadmin:
The last and final layer to handle errors are in the CFADMIN section.
The Site-wide Error Handler: for site-wide error handler file.
The Missing Template Handler: for missing template handler Sequence for error handling:
1. | cftry/cfcatch |
2. | Application.cfc :: onError() |
3. |
Application.cfc :: OnRequest() :: with CFERROR tags OR Application.cfm with CFERROR tags |
4. | CFADMIN settings |
1. | Application.cfc :: onMissingTemplate() |
2. | CFADMIN settings |
ColdFusion Error Handling
<cfcomponent output="false">
<cfscript>
this.name = "errorHandling";
</cfscript>
...
<cffunction name="onRequestStart" returnType="boolean" output="true">
<cfargument name="targetPage" type="String" required="true" />
<!--- 3rd layer error handling [last and final being the CFADMIN settings] --->
<cferror type="exception" template="errorTemplates\exception.cfm" exception="any">
<cferror type="request" template="errorTemplates\request.cfm" exception="any">
<cferror type="validation" template="errorTemplates\validation.cfm" exception="any">
<cfreturn true />
</cffunction>
...
<!--- handle 404 errors --->
<cffunction name="onMissingTemplate" returnType="boolean" output="true">
<cfargument name="targetpage" type="String" required="true" />
<cfset var retval = true />
<cftry>
<cfheader statusCode="404" statusText="Page Not Found" />
<cfinclude template="errorTemplates\missingInclude.cfm">
<!--- return true prevents the default ColdFusion error handler from running --->
<cfset retval = true />
<cfcatch type="any">
<!--- return false will allow ColdFusion's default error handler to run and
this will force the CFADMIN to display the missing template file (if present) --->
<cfset retval = false />
</cfcatch>
</cftry>
<cfreturn retval />
</cffunction>
<!--- handle script/500 errors
2nd layer error handling [1st being CFTRY/CFCATCH] --->
<cffunction name="onError" returnType="void" output="true">
<cfargument name="exception" required="true" />
<cfargument name="eventname" type="String" required="true" />
<cfsavecontent variable="errorData">
<cfoutput>
EventName: #arguments.eventName#
| Detail: #arguments.exception.detail#
| Message: #arguments.exception.message#
| StackTrace: #arguments.exception.stackTrace#
| Type: #arguments.exception.type#
| name: #arguments.exception.name#
| template: #arguments.exception.tagContext[1].template#
| line: #arguments.exception.tagContext[1].line#
| rawtrace: #arguments.exception.tagContext[1].raw_trace#
</cfoutput>
</cfsavecontent>
<cflog type="error" application="true" file="onErrorData" text="#errorData#" />
<cfinclude template="errorTemplates\onError.cfm">
<!--- throw an error to allow ColdFusion's default error handler to run and
this will force the CFADMIN to display the site wide error template file (if present)
<cfthrow message="#errorData#" /> --->
<cfreturn />
</cffunction>
</cfcomponent>
Ginger CMS
the future of cms, a simple and intuitive content management system ... ASP.NET MVC Application
best practices like Repository, LINQ, Dapper, Domain objects ... CFTurbine
cf prototyping engine, generates boilerplate code and views ... Search Engine LITE
create your own custom search engine for your web site ... JRun monitor
monitors the memory footprint of JRun engine and auto-restarts a hung engine ... Validation Library
complete validation library for your web forms ...
the future of cms, a simple and intuitive content management system ... ASP.NET MVC Application
best practices like Repository, LINQ, Dapper, Domain objects ... CFTurbine
cf prototyping engine, generates boilerplate code and views ... Search Engine LITE
create your own custom search engine for your web site ... JRun monitor
monitors the memory footprint of JRun engine and auto-restarts a hung engine ... Validation Library
complete validation library for your web forms ...