3 Replies Latest reply on Jun 18, 2009 11:27 AM by milesian

    onError function in Application.cfc

    milesian
      While I use all the new functions that come with MX in the Application.cfc I still don't have a good feel for some of them. Below is roughly what I use for the onError function based on other peoples code and my own experience. With some tweaking it could be used as a generic function.

      <cffunction name="onError" returntype="void" access="public" hint="Fires when an error is generated that is not caught by cftry / cfcatch">

      <!--- gives info on what the error is --->
      <cfargument name="exception" type="any" required="true">
      <cfset var Except = ARGUMENTS.exception>


      <!--- if sessionExpiration becomes true redirect to local home page--->
      <cfset var SessionExpiration = false>
      <cfset var redirectUrl= "/index.cfm">


      <!--- exit if exception is caused by a cfabort tag or event handler --->
      <cfif (ARGUMENTS.exception IS "coldfusion.runtime.AbortException")
      OR (ARGUMENTS.exception IS "coldfusion.runtime.EventHandlerException: Event Handler Exception.") >
      <cfreturn/> </cfif>


      <!--- Does this sort out the session variables after a person has logged out? --->
      <cfif StructKeyExists(Except,"RootCause") AND StructKeyExists(Except["RootCause"],"Detail")>
      <cfif Except["RootCause"]["Detail"] CONTAINS "Session is invalid">
      <cfset SessionExpiration = true>
      </cfif>
      </cfif>

      <!--- using the j_username paramters in login can cause
      confusion about session status after logout - tidy up by
      definitely expiring the session and redirecting --->
      <cfif SessionExpiration>

      <cfcookie name="JSESSIONID" value="" expires="NOW">
      <cfheader statuscode="302" statustext="Moved Temporarily">
      <cfheader name="location" value="#redirectUrl#">


      <cfelse> <!--- a real error show message and contact adminsitrator --->

      <!--- this is supposed to hand over control of the error to the cferror tags --->
      <cfmail to="#REQUEST.siteAdmin#" from="someEmail@someserver.com"
      subject="Error Diagnostics" server="#REQUEST.mailServer#">
      There is an error on: #REQUEST.ThisAddress#
      ...................................................
      The brief detail is: #Except#
      </cfmail>

      <!--- hand over to cferror tags --->
      <cfthrow object="#arguments.exception#">

      </cfif> <!--- sessionExpiration --->

      </cffunction>

      <cferror type="exception" template="error_messages/general_error.cfm">
      <cferror type="validation" template="error_messages/validationError.cfm" >
      <cferror type="request" template="error_messages/general_error.cfm">
        • 1. Re: onError function in Application.cfc
          milesian Level 1
          Apologies, I submitted before I got to ask the questions
          1) Why are the cfabort and handler exceptions appearing when they didn't using just Application.cfm and the cferror tag?
          2) Does the above code sort out the status of session variables after a person logs out? If not can it be dealt with in this function?
          3) At the moment I don't use the cfthrow and cferror tags because I keep getting a template not found error despite the same code having worked fine in Application.cfm
          I put this up as I'm sure other people must be running into similar issues. All comments welcome!
          • 2. Re: onError function in Application.cfc
            milesian Level 1

            Even though the exceptionError.cfm template is in the root folder it will give a template not found error message. The solution is to setup a mapping in the Coldfusion administrator and point it at the root folder.

             

            The cferror tag in the Application.cfc should look something like:

             

            So <cferror template="/#THIS.rootMapping#/exceptionError.cfmtype="exception">

             

            Don't forget the first forward slash.

            • 3. Re: onError function in Application.cfc
              milesian Level 1

              The cferror problem was the main issue and it now works.