Use an application wide error handler
February 18, 2010
I've been working on a application that another developer wrote and have been posting my tweaks into what seems to have turned into a mini series! Whilst I'm not posting anything new, I figured that there might be other people out there who don't know about these things. So, this time I've added a application wide error handler which notifies me of errors.
The idea here is that instead of having your angry client ring up and tell you that their site is broken (and that is often as much information you get to help you track down the bug!) you'll be notified via email with some useful information everytime anyone gets an untrapped error on the site. What do I mean by untrapped? Well if your code has try and catch code blocks, then you're already got code to handle errors in your application. Untrapped errors are when a bit of code throws an exception that isn't wrapped in try and catch block.
Adding an application wide error handler is really easy in ColdFusion 8. All you need to do is add the make use of the in-built onError method of Application.cfc.
Here is an example of a simple Application.cfc with the onError method:
<cfcomponent output="false"
hint="A really simple Application.cfc with error handling">
<cfset this.name = "myapp_" & Hash( GetCurrentTemplatePath() )>
<cffunction name="onError"
returntype="void"
output="true"
access="public"
hint="I am executed when an unhandled error occurs">
<cfargument name="exception"
type="any"
required="true">
<cfargument name="eventName"
type="string" required="true">
<!--- oh no! we have an error, show a nice message to the user --->
<cfoutput>
<h2>Sorry, something went wrong!</h2>
<p>Don't panic as some of the finest brains are looking into it right now.</p>
</cfoutput>
<!--- send an email --->
<cfmail to="itwentbang@some.domain"
from="itwentbang@some.domain"
subject="Error: #this.name#"
type="html">
<!--- dump the exception --->
<cfdump var="#arguments.exception#" label="exception">
<!--- dump some other useful info, could include session, cookies etc --->
<cfdump var="#form#" label="form">
<cfdump var="#url#" label="url">
<cfdump var="#CGI#" label="CGI">
</cfmail>
</cffunction>
</cfcomponent>
There you go, error notification and a nicer error message to show users. Don't be like one developer (who shall remain nameless who said (and I'm paraphrasing slightly); "I don't like getting email error notifications - it fills up my inbox".
I should point out that you may end up with some fairly large emails turning up in your inbox so I'd recommend you play around with it, but this is a good starting point. The onError method I currently use doesn't use cfdump and parses out the stacktrace and framework information, but everyone has their own preferences.
If you want to know more about Application.cfc I can recommend Ben Nadel's post and the ColdFusion Jedi's Application.cfc reference
- Posted in:
- ColdFusion
1 comment
Leave a comment
If you found this post useful, interesting or just plain wrong, let me know - I like feedback :)





I ended up using the hopToadnotifier.cfc to manage my errors.
github.com/shayne/coldfusion-hoptoad-notifier
/>
I also forked one that works on BlueDragon and older versions of ACF.
github.com/CFPROD/coldfusion-hoptoad-notifier
Comment by Kevin – March 03, 2010