HJCotton.net

July 13, 2009

cfcatch was born a struct, then it wasn’t but now it’s back

by @ 9:19 pm. Filed under Coldfusion.

Recently, I came across an interesting “catch” when passing around a cfcatch structure. I created a function that would take a cfcatch struct and send out an email with the results formatted as I wanted them to be:

<cfcomponent output="false">
  <cffunction name="sendCFCatchEmail" access="public" output="false" returntype="void">
    <cfargument name="cfcatchStruct" type="struct" required="yes">
    <cfargument name="emailAddress" type="email" required="yes">
 
    <cfmail from="email@email.com" subject="Error" to="#arguments.emailAddress#" type="html">  
      <table>
        <tr>
          <cfoutput>
            <td>#cfcatch.type#</td>
            <td>#cfcatch.message#</td>
            <td>#cfcatch.detail#</td>
          </cfoutput>  
        </tr>
      </table>
    </cfmail>
 
  </cffunction>
</cfcomponent>

What threw me was that I kept getting the following error when passing a cfcatch struct to the function:

“The CFCATCHSTRUCT argument passed to the sendCFCatchEmail function is not of type struct.”

Huh?

The difficulty I was having was that when you dump a cfcatch, it identifies itself as a struct.

cfcatch

It took me a little while (and maybe some googling) to figure out what was going on. Doing an “isStruct(cfcatch)” shows that for some reason, cfcatch is not a structure. What is it then? Well, an “isObject(cfcatch)” shows that it’s an object… of some kind. The particularly odd part is that “Duplicate(cfcatch)” will return a structure, not an object. Weird, huh?

<cftry>
  <cfset answer = 3 / "t">
 
  <cfcatch>
    <cfoutput>
      <h2>Standard cfcatch</h2>
      Is Struct: #isStruct(cfcatch)#<br />
      Is Object: #isObject(cfcatch)#<br />
 
      <h2>Duplicated cfcatch</h2>
      Is Struct: #isStruct(Duplicate(cfcatch))#<br />
      Is Object: #isObject(Duplicate(cfcatch))#<br />
 
    </cfoutput>
  </cfcatch>
</cftry>

cfcatch Output

In short, I just ended up changing the expected type for the cfcatchStruct argument to be “any”. I could duplicate cfcatches before sending to the function, but that’s not optimal in the least, so I just let cfcatch pretend that it’s an object if that’s what it wants to be.

One Response to “cfcatch was born a struct, then it wasn’t but now it’s back”

  1. ingrid newcomer Says:

    when exactly did you forget how to speak english?

Leave a Reply

HJCotton.net

internal links:

categories:

search blog:

archives:

February 2010
S M T W T F S
« Dec    
 123456
78910111213
14151617181920
21222324252627
28  

So it goes.
— Kurt Vonnegut

general links:

IMG_0515.jpg

IMG_0515.jpg

11-22-2009

11-22-2009

other:


25 queries. 1.602 seconds