Simple Coldfusion Error Catching

Jan 31, 2007
This should work on CF7: It's quite generic, but essential. With the example below, add these two functions to your application.cfc and create a template to use for when the error is thrown. This example sends you an email with a dump of all the necessary vars used to troubleshoot. You could of course use CFFILE to write a line to an html file instead of mailing. Also, dependent on your hosting, some shared hosts may restrict the dumpling of cgi vars ( you'll get a sandbox.security error instead). Add this to your application.cfc "onRequest"> "targetpage" required="yes"> "exception" message="Test Error"> "#targetpage#"> "onError" returntype="void" output="true"> "exception" required="yes"/> "eventName" type="string" required="yes"/> "type") AND arguments.exception.RootCause.type EQ "coldfusion.runtime.AbortException"> "onApplicationEnd" AND EventName NEQ "onSessionEnd" OR len(url.error)> "test@domain.com" from="test@domain.com" server="smtp.domain.com" subject="Error: #arguments.exception.rootcause.Message# " type="html" spoolenable="yes"> Error occurred: #Now()#
"#arguments.exception#" label="Exception"/> "#cgi#" label="CGI Variables"/> "#form#" label="Form Variables"/> "#url#" label="URL variables"/>
"PageNotFound"> "/extensions/error/404.cfm"> "/extensions/error/error.cfm">
Now create your Error Handling page: (I've used /extensions/error/ as the folder, but there's no limit to where you can place it.

Error

An unexpected error has occured.

"Message")>

#arguments.exception.rootcause.Message#

p>The error has been logged and will be dealt with as soon as possible.

If this problem persists, please "/contact/">Contact Us.

Now test by going to a URL such as www.domain.com/?error=1 P.s you may want to add url.error as a cfparam.

Comments

Write your comment



(it will not be displayed)