Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CFCATCH throwing error in CFC

Tags:

coldfusion

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

like image 512
Rob M Avatar asked Feb 17 '26 16:02

Rob M


1 Answers

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>
like image 70
BKK Avatar answered Feb 19 '26 09:02

BKK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!