• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

OnError in application.cfc

Guest
Oct 22, 2009 Oct 22, 2009

Copy link to clipboard

Copied

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.

TOPICS
Advanced techniques

Views

3.9K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 22, 2009 Oct 22, 2009

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 23, 2009 Oct 23, 2009

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 25, 2009 Oct 25, 2009

Copy link to clipboard

Copied

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>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 28, 2009 Oct 28, 2009

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 28, 2009 Oct 28, 2009

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 29, 2009 Oct 29, 2009

Copy link to clipboard

Copied

This is my onError code

<cffunction name="onError" returnType="void" output="true">
         <cfargument name="exception" required="true">
       
        <cfinclude template="incl_onError.cfm">

</cffunction>

This is my incl_onError.cfm page content

<cfif APPLICATION.applicationmode EQ "production">
    <cfset errorMessage= arguments.exception>
    <cfset errorType="">
    <cfset errorDetail=arguments.exception.cause.Message>
    <cfset tagContext=arguments.exception.tagContext>     

    <cfset customErrorMessage= 'error in: ' & arguments.exception.cause.Message & ' Accessed Page: ' & cgi.script_name & ' Referred page: '& cgi.http_referer>     

    <cfset errorFileLocation = application.ErrorManager.errorLogDetails(errorMessage, errorType, errorDetail, tagContext)>
   
    <cfif session.createdTicket NEQ 1>
       
        <cfset application.EmailsManager.emlHelpdesk(application.adminemail, SESSION.strEmail, application.applicationname, APPLICATION.applicationowner,APPLICATION.applicationdeveloper, SESSION.strFullName, SESSION.strPhone, customErrorMessage, errorFileLocation)>
    </cfif>

    <cflocation url="#application.siteroot#error.cfm" addToken="false">
<cfelseif APPLICATION.applicationmode EQ "development">
       
    <cfdump var="#arguments.exception#"/>
    <cfdump var="#form#"/>
    <cfdump var="#url#"/>
</cfif>

I'm setting the applicationmode in application as production. Now the email triggers but the user does not see the error.cfm page.

Thanks,

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 29, 2009 Oct 29, 2009

Copy link to clipboard

Copied

Have you verified that all those variables in the conditional statements have the value that they need to have for the logic to execute?  I mean check them in the actual code, at the time the code executes.

Stick a couple of <cflog> statements in just before each conditional, and log the current value of those variables.

--

Adam

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 29, 2009 Oct 29, 2009

Copy link to clipboard

Copied

<cfif session.createdTicket NEQ 1>     
       <cfset application.EmailsManager.emlHelpdesk(application.adminemail, SESSION.strEmail, application.applicationname, APPLICATION.applicationowner,APPLICATION.applicationdeveloper, SESSION.strFullName, SESSION.strPhone, customErrorMessage, errorFileLocation)>
    </cfif>

    <cflocation url="#application.siteroot#error.cfm" addToken="false">

First, onError has two arguments. One is Exception, the other Event. So, add the line

<cfargument name="EventName" type="String" required="true">

If Coldfusion fails to reach the cflocation tag, that would suggest that the function emlHelpdesk failed to return control to the point just before the cfif end-tag.Test this, for example, by placing the following logging line just before the end-tag </cfif>:

<cflog file="onErrorTestLog"  text="Function emlHelpdesk has returned control to onError.">

Remember also that onError runs only within the context of a live session and live application. Therefore, you should replace the line

<cfif session.createdTicket NEQ 1>

with the line

<cfif  NOT (Arguments.EventName IS "onSessionEnd" OR Arguments.EventName IS "onApplicationEnd") AND session.createdTicket NEQ 1 >

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 19, 2019 Apr 19, 2019

Copy link to clipboard

Copied

Hello,

can u help me with onError method.i am not getting what excactly to write code in onError method.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 19, 2019 Apr 19, 2019

Copy link to clipboard

Copied

See my reply to Sanjeet's same question posted here also today.


/Charlie (troubleshooter, carehart.org)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 21, 2019 Apr 21, 2019

Copy link to clipboard

Copied

what all exception that we can handle in onError method? can you please help me with any coding example..

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 22, 2019 Apr 22, 2019

Copy link to clipboard

Copied

LATEST

Hi sangeetab51931146

This thread is over 9 years old, hence quite old. Besides, your question is different from the original one. Therefore, I would advise you to start a new thread for your question. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation