8 Replies Latest reply on Oct 29, 2009 5:09 PM by BKBK

    OnError in application.cfc

    naveenbalraj

      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.

        • 1. Re: OnError in application.cfc
          Adam Cameron. Level 5

          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

          • 2. Re: OnError in application.cfc
            naveenbalraj Level 1

            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.

            • 3. Re: OnError in application.cfc
              BKBK Adobe Community Professional & MVP
              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>

              • 4. Re: OnError in application.cfc
                naveenbalraj Level 1

                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.

                • 5. Re: OnError in application.cfc
                  BKBK Adobe Community Professional & MVP
                  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.

                  • 6. Re: OnError in application.cfc
                    naveenbalraj Level 1

                    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,

                    • 7. Re: OnError in application.cfc
                      Adam Cameron. Level 5

                      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

                       

                       

                       

                       

                       

                       

                      • 8. Re: OnError in application.cfc
                        BKBK Adobe Community Professional & MVP

                        <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 >