6 Replies Latest reply on Nov 16, 2007 4:35 AM by stevenbusse

    handle throws with cferror

    stevenbusse
      I would like to create an error handling system in our application. but I just cant get a simple prototype to work as it should.

      lets say, I want to handle two kinds of errors by throwing exceptions. in case of the sendToDev exception I want an email to be sent to the developers. In case of the sendToSupport exception I want an email to be sent to the support.

      Therefore, I define two kind of cferrors:

      <cferror type="exception"
      exception="sendToDev"
      mailto="dev@foobar.com"
      template="handleError.cfm">

      <cferror type="exception"
      exception="sendToSupport"
      mailto="support@foobar.com"
      template="handleError.cfm">

      on the handleError.cfm the mailto-attribute will be used to generate the email to the right adress.

      so far for the cferror-definitions in application.cfm. now the throwing inside of some function in a cfc:

      <cfif [condition]>
      <cfthrow type="sendToDev" message="an error occured. mail to developers has been sent">
      <cfelse>
      <cfthrow type="sendToSupport" message="an error occured. mail to supporters has been sent">
      </cfif>

      my understanding of this:

      depending on what I've defined as "type" when throwing the exception, the according cferror-tag should be used by the application. but it doesnt. whatever the condition is and whatever exception is thrown, it's allways the last defined cferror-tag which will be used by the application. in the described case, the sendToSupport-cferror will be used whatever is thrown. if i would define the sendToSupport-cferror first in the application-file, the sendToDev would be used.

      any help here? i just dont get it :-(

      regards

      steven
        • 1. Re: handle throws with cferror
          Dan Bracuk Level 5
          I think you will have better luck switching from application.cfm to application.cfc. Then instead of using cferror, you can put whatever code you want into the onError function.
          • 2. Re: handle throws with cferror
            stevenbusse Level 1
            hello and thanks for the reply.

            we're actually using coldfusion mx 6, so I can't go for application.cfc at the moment, as it will take at least another half year until we start migration directoly to coldfusion 8.

            therefore, I'd still be very pleased if you have any suggestions on my issue, as I really dont know if I'm missing something there. As for the custom exception: I dont have to create any java classes or components for the exceptions, right? only give the exceptions name when throwing and checking for it on cferror. that should be it, right?
            • 3. Re: handle throws with cferror
              Daverms Level 3
              Hi,

              What condition you have before your <cfthrow> statements?
              • 4. handle throws with cferror
                stevenbusse Level 1
                <cfif arguments.number lt 16>

                very simple. only prototyping.

                input is 12. so the sendToDev is throwed and the first cferror with mail dev@foobar.com should react. but the other one does. also, if the input is 17.

                and if i declare the cferrors the other way round:


                <cferror type="exception"
                exception="sendToSupport"
                mailto="support@foobar.com"
                template="handleError.cfm">

                <cferror type="exception"
                exception="sendToDev"
                mailto="dev@foobar.com"
                template="handleError.cfm">

                then, the sendToDev cferror is always called, not mattering what input I give.
                • 5. Re: handle throws with cferror
                  Daverms Level 3
                  Hi,

                  Try something like this,
                  • 6. handle throws with cferror
                    stevenbusse Level 1
                    unfortunately, it doesnt change anything.

                    maybe I should just post you the whole prototype application as i run it here. i tried to keep it as simple as possible and added a short comment to describe what file is shown in the code, so you dont have to read thru a big bunch of code.

                    in this example with the input value 12, the output of the handleError.cfm looks like this:

                    msg: 12 - mail sent to dev.
                    mailto: support@foobar.com

                    of course, this is not what I would like to see. if cferror would react as wanted, the answer mailto should be dev@foobar.com