An Architect's View

ColdFusion, Software Design, Frameworks and more...

An Architect's View

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

    I use this code as a workaround in the onError function in my application.cfc

    <cfif arguments.Exception.Type eq "coldfusion.runtime.AbortException">
    <cfreturn>
    </cfif>
  • 3 Jason Daiger // Jun 1, 2006 at 6:26 PM

    I had been hit by this same issue for several weeks and finally got around to fixing it just last Thursday. I found 2 excellent blog entries that provide great solutions for the fix.
    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

    This happens to me and I DON'T have the onRequest() method as stated above in Sean's post.
    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

    I had this problem too! in application.cfc with an onRequestStart function and using cfabort if session did not exist the onError function was being called by cfabort..... I modified Jos's example and it worked great:

    <cfif arguments.Exception.RootCause.Type eq "coldfusion.runtime.AbortException"> <cfreturn> </cfif>
  • 6 Abram // Apr 27, 2009 at 4:21 PM

    I know this is an old post, but was this "fixed" in cf8? I have a dev box that is cf8 and a production box that is cf7. The work-around mentioned here trapping the AbortException in the onError method does not work in cf8, but works in cf7. In fact, no error is raised on cfabort in cf8 unless you use the showError attribute.

Leave a Comment

Leave this field empty: