Skip to main content
October 22, 2009
Question

OnError in application.cfc

  • October 22, 2009
  • 2 replies
  • 4385 views

Hi All,

I have a wierd problem. We had this application, which was running using application.cfm and now we changed to application.cfc. Everything is looking and running fine. One feature we added is to make the OnError function to log the error in the DB and show a customer friendly error.

The issue: For some reason, the onError triggers the error and logs the error into the DB. But the user is never interrupted.  Any idea?. The error logged is variable is undefined. But I know, we have set the cfparam and default value as "". Not sure, if that was causing the problem, so I changed that to "0".

I thought, onError will stop the application to run further, if any error found.

I have posted this question to Ray as well. Just posting here to get others thought.

This topic has been closed for replies.

2 replies

BKBK
Community Expert
Community Expert
October 25, 2009
The error logged is variable is undefined. But I know, we have set the cfparam and default value as "". Not sure, if that was causing the problem, so I changed that to "0".

The error message suggests that the cfparam isn't in Coldfusion's memory when the error occurs. Trace the steps back, from where the error occurs to the cfparam tag.

I thought, onError will stop the application to run further, if any error found.

It will, if the error occurrs in one of Application.cfc's methods. Otherwise, it will work as Adam has described.

You should consider using the cferror tag, in place of onError. It might fit your requirements better. If you decide to do so, you should locate it in Application.cfc as in the following example:

<cfcomponent displayname="MyApp application file">
    <cfscript>
        this.name = "myApp";
        this.applicationTimeout = "#createTimespan(1,0,0,0)#";
        this.loginStorage = "session";
        this.sessionManagement = "true";
        this.sessionTimeout = "#createTimeSpan(0,0,20,0)#";
   </cfscript>
   <cferror exception="any" type="exception" template="relative_path_to_error_page.cfm">
<cffunction name="onApplicationStart" returntype="boolean">
    <cfreturn true>
</cffunction>

<cffunction name="onSessionStart">
</cffunction>
<!--- other events, etc., etc., excluding onError --->
</cfcomponent>

October 29, 2009

Hi all,

thanks for the inputs and suggestions.

My onError is part of application.cfc, still  could not understand why it triggers and never relocates to the page it is suppose to.

Does warning of any variables (if it is not defined, as I said we set the value as cfparam default="" and not any values) just triggers the onError and proceeds, without stop.

Please share your thoughts.

BKBK
Community Expert
Community Expert
October 29, 2009
why it triggers and never relocates to the page it is suppose to.

There can be only one reason. Coldfusion's execution flow never reaches the cflocation tag. Look at your code again. Paste it here if you need other pairs of eyes.

Inspiring
October 23, 2009

onError() does only what you tell it to do.  It does not automatically cause the app to halt... what if that's not how you want your errors to be handled?  The whole idea of the handler is that the default behaviour (output the exception details and stop processing) can be overriden with other - custom-written - behaviour.  So the handler makes no assumptions about what it should be doing.

If you want the thing to stop, either <cfabort> or rethrow the exception (depending on circumstance).

--

Adam

October 23, 2009

Thanks Adam,

In my OnError function, I have a relocation page to show the user customized error. So as per the code, when the OnError is triggered, the application is suppose to relocate to the custom page (that I included as template in the Onerror function). Right now, that is not happening in my app.

But this situation is not happening for all the situations. For some situations, it is showing the customised template and does not proceed. But one rare case, this is happening, as I explained in my original question.

Do you mean to say, if I add cfabort right after the template include, that should work? Will try that.