Skip to main content
Participant
February 18, 2014
Question

getting error message - onError undefined - even though onError is handling the error

  • February 18, 2014
  • 2 replies
  • 1103 views

I'm setting up a new app in CF 10, and have set up my application.cfc. I created a test page that calls onError() manually, as a test. Like the title says, I get a message saying "onError is not defined", but onError() itself is handling this error. I know this because I have it output a string to indicate that it has fired.

Also, I'm trying to test onError() by intentionally not closing a tag and calling the page. onError() won't fire here; instead I get the standard ColdFusion error grey box.

Any help on these is much appreciated.

    This topic has been closed for replies.

    2 replies

    BKBK
    Community Expert
    Community Expert
    February 18, 2014

    Just a remark next to what Carl has said. The methods of Application.cfc start with the prefix 'on', meaning, 'upon'. That is to say, the methods are event handlers. For example, onError runs upon an error occurring. That is designed to happen automatically, without your intervention.

    However, nothing stops you from calling any of Application.cfc's event handlers manually. Suppose you attempt to call onError from a CFM page. Then that will mean first creating an Application component object to call it on. At this point you will find yourself having to create an instance of Application.cfc. My guess is, you had an error because you failed to create that object.

    But then comes the question: why muck about creating an instance of Application.cfc in a CFM page, when the CFC is designed to run as an include that precedes the page? In other words, instantiating Application.cfc defeats the very purpose of its design.

    Xophe99Author
    Participant
    February 18, 2014

    Thanks Carl and BK -- I see what's happening. However, I believe that an instance of Application.cfc is instantiated when the application starts, else how could the event handlers listen and fire when they should? Also, you can call Application.cfc's functions on all cfm pages via the variables scope:

    http://www.bennadel.com/blog/385-Application-cfc-Scoped-Functions-Are-Stored-In-Page-VARIABLES-Scope.htm

    I just manually called onError() on a cfm page passing the required arguments and it ran fine.

    Xophe99Author
    Participant
    February 18, 2014

    I take that back -- my onError() didn't fire because I called it, it fired because onError() wasn't defined.. And I tried running Ben's code from the link above, but I got a "method not defined" error. This even though I set access=public for both methods. Maybe that's something new with CF 10.

    Carl Von Stetten
    Legend
    February 18, 2014

    I don't think you can call onError() directly, as it only exists at the application level (just like you can't call onRequest() or other methods outside of Application.cfc).  Also, onError() only traps runtime errors, not compilation errors.  Leaving a tag unclosed is a syntax error that is thrown during compliation, not during runtime.  Instead, to test onError() try referencing a variable that doesn't exist or executing a built-in function with invalid parameters.

    -Carl V.