error handling

Ketan Jetty
enthusiasm for technology

error handling

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 is used for programmatic handling of errors.

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.

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:
2.Application.cfc :: onError()
3.Application.cfc :: OnRequest() :: with CFERROR tags
Application.cfm with CFERROR tags
4.CFADMIN settings

Sequence for missing files or templates:
1.Application.cfc :: onMissingTemplate()
2.CFADMIN settings

download sample code

ColdFusion Error Handling
<cfcomponent output="false">
	<cfscript> = "errorHandling";

	<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 />


	<!--- handle 404 errors --->
	<cffunction name="onMissingTemplate" returnType="boolean" output="true">
		<cfargument name="targetpage" type="String" required="true" />
		<cfset var retval = true />
		    <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 />

		<cfreturn retval />

	<!--- 	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">
			  EventName: #arguments.eventName# 
			| Detail: #arguments.exception.detail# 
			| Message: #arguments.exception.message# 
			| StackTrace: #arguments.exception.stackTrace# 
			| Type: #arguments.exception.type# 
			| name: 
			| template: #arguments.exception.tagContext[1].template# 
			| line: #arguments.exception.tagContext[1].line# 
			| rawtrace: #arguments.exception.tagContext[1].raw_trace# 

		<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 />


CF Quick Reference

Ginger CMS
the future of cms, a simple, easy and intutive content management system ... more

cf prototyping engine, generates boilerplate code and ... more

Jrun monitor
monitor and timely auto-restart to avoid Jrun hang ... more

Inheritance Config.
uses OOPs inheritance to create configuration file ... more

Real Estate App.
complete real estate application using data from MLS ... more

Search Engine Lite
create your own search engine for your web site ... more