onError(), onRequest() and cfabort
May 30, 2006 · 6 Comments
Several people have noticed that if you are using Application.cfc and have an onError() method in it, when you use cfabort to end a request, it will trigger the onError() method. This seems pretty counter-intuitive to a lot of people so I figured I'd post some clarifications.
First, you only get the error if you also have an onRequest() method in Application.cfc. If you have no onRequest() method and you use cfabort, then onError() will not be called.
Second, if you read the docs for cfabort it should be clear that, in general, cfabort is considered to be an error condition. The docs are very explicit about the interaction between cfabort, cferror and the presence or absence of the showError= attribute on cfabort.
If you don't have onError() and you use the showError= attribute, then cfabort will terminate execution of the request and display the error to the user (as expected).
If you do have onError() and you use the showError= attribute, then cfabort will terminate execution of the request and onError() will be invoked (as expected).
So the edge case to watch for is where you have both onRequest() and onError() and you do not have showError=. Caveat programmer.
Tags: coldfusion

6 responses so far ↓
1 Jeff Houser // May 30, 2006 at 7:52 PM
It might be worth nothing that cflocation will call the OnError event also. In my experience, this only happens if the cflocation is in the Application.cfc.
I read about this in Blogs before, so believe this is a known bug.
2 Jos // May 31, 2006 at 12:05 AM
<cfif arguments.Exception.Type eq "coldfusion.runtime.AbortException">
<cfreturn>
</cfif>
3 Jason Daiger // Jun 1, 2006 at 6:26 PM
1. Steve Bryant's blog (steve.coldfusionjournal.com) entry on 3/22/2005 titled 'Hide Your Errors (Application Events: onError)'
2. Ray Camden's blog (ray.camdenfamily.com) entry on 2/7/2005 titled 'Warning About Application Events'
Now just to find a solution for the onSessionEnd and onApplicationEnd errors that are getting thrown. Any ideas?
4 Glenda Vigoreaux // Aug 1, 2006 at 5:07 PM
I used a solution similar to Jos' idea (thanks for the input).
My code looks like this:
<cfif arguments.Exception.RootCause.Type is "coldfusion.runtime.AbortException">
<cfreturn true>
</cfif>
Remember your returntype="void" in your onError() method, so don't return "true".
5 Andrew Grosset // Dec 12, 2007 at 7:21 AM
<cfif arguments.Exception.RootCause.Type eq "coldfusion.runtime.AbortException"> <cfreturn> </cfif>
6 Abram // Apr 27, 2009 at 4:21 PM
Leave a Comment