Rob M
Rob M

Reputation: 1031

CFCATCH throwing error in CFC

For some reason, a piece of code that works fine on a *.cfm page, and did work fine in a *.cfc, now is throwing an error when error is detected.

The error is:

Element SQL is undefined in CFCATCH. 

The code block where this is getting throw looks like this:

<cfcatch type="database">
    <cfset errorMessage = "
        <p>#cfcatch.message#</p>
        <p>Please send the following to a developer:</p>
        <p>#cfcatch.SQL#</p> <--- ERROR HERE
        <p>#cfcatch.queryError#</p>
        <p>#cfcatch.Where#</p>">
    some other stuff
</cfcatch>

Any thoughts?

UPDATE

Using @BenKoshy suggestion, I modified my <cfcatch> statement.

Remember K.I.S.S.? Keep It Simple Stupid

Using his method and then modifying it, I was getting more data back than I was going use, so I went with a simple version, and it works as advertised.

<cfif isDefined("cfcatch.message")>
  <cfset errorMessage = errorMessage & "<p>#cfcatch.message#</p>">
</cfif>
<cfif isDefined("cfcatch.SQL")>
    <cfset errorMessage = errorMessage & "<p>Please send the following to a developer:</p><p>#cfcatch.SQL#</p>">
</cfif>
<cfif isDefined("cfcatch.QueryError")>
    <cfset errorMessage = errorMessage & "<p>#cfcatch.queryError#</p>">
</cfif>
<cfif isDefined("cfcatch.Where")>
    <cfset errorMessage = errorMessage & "<p>#cfcatch.Where#</p>">
</cfif>

Nice and easy and it works. KISS

Upvotes: 3

Views: 2402

Answers (1)

BKK
BKK

Reputation: 2073

Just means the error data did not contain an SQL statement. Shouldn't assume that variable will exist for all errors:

<cfif isDefined("cfcatch.sql")>
     <p>#cfcatch.SQL#</p>
 </cfif>

Is the easy fix. Probably best to loop through the struct like this:

<cfparam name="errorMessage" default="">
<cfloop collection="#cfcatch#" item="this">
    <cfset errorMessage = errorMessage & "<p>#cfcatch[this]#</p>">
</cfloop>

Upvotes: 6

Related Questions