There are many times while developing ColdFusion or Flash Remoting (especially) applications that the default log files and error messages supplied by ColdFusion and/or the NetConnection debugger don't give you enough information. For those situations, the technique shown in this article will show more than the typical amount of error info.

Sending the Error to Yourself

There are several ways to record the error as it occurs. The first technique will use the <cfmail> tag. The error info will go out in an email to you as the site administrator. I prefer to get the messages in my inbox because it happens the moment an error occurs so that I can go into my application and find the problem.

The technique requires the use of a <cftry> and <cfcatch> block. The <cfcatch> block exposes a structure called cfcatch that gives you a tremendous amount of information about the error that occurred. If you've ever seen a Flash Remoting error like this, you'll begin to appreciate the cfcatch version:

Error: A database error has occurred.

If you go into the log files or look at the NetConnection debugger, you'll see the same cryptic message. Now compare that with the error info that a cfcatch struct will give you:

Error File

The error messages give you, among other things:

Using this detailed information you can make a better informed decision on how to fix the error.

The steps required to include this type of error handling in your application should not vary that much from the actual steps that you take to trap errors: in other words, you should already be using <cftry> and <cfcatch> tags in your application. Adding the code necessary to email yourself the error (or write it to a file) will not be that hard.

Taking a query as an example, add a <cftry> and <cfcatch> tag around it to capture the error:

<cftry>
  <cfquery name="rsBlah" datasource="MyDSN">
   SELECT * FROM MyTable WHERE MyID = #id#
  </cfquery>
  
  <cfcatch>
    <!--- Error occurred, send the email --->
    <cfmail to="me@mydomain.com"
      from="me@mydomain.com"
      subject="Error in MyAPP"
      type="html">
       <h1>Error occurred:</h1>
       <cfdump var=#cfcatch#>

    </cfmail>

    <cflocation url="myErrorPage.cfm">
  </cfcatch>
</cftry>

In an actual application, you may already have the <cftry> block in place with a <cflocation> tag to deliver the end user to an error page. Alternatively, your <cftry> block might simply write to a log file instead, or simply do nothing. The <cfmail> block is now added to send the cfcatch structure out in an email body to you. The type of the email is set to HTML so that it shows the full structure of the cfcatch block, including the HTML formatting and JavaScript that goes with it.

Writing the Error to a File

To write the same information to a file rather than send in an email, you could do it like this:

<cftry>
  <cfquery name="rsBlah" datasource="MyDSN">
   SELECT * FROM MyTable WHERE MyID = #id#
  </cfquery>

  <cfcatch>
    <cfsavecontent variable="tempError">

      <!--- Dump the cfcatch struct --->
      <cfdump var=#cfcatch#>

    </cfsavecontent>
    
    <!--- Create a filename --->
    <cfset filename="#DateFormat(Now(),'MMDDYYYY')#-#TimeFormat(Now(),'hhmmssl')#.html">

    <!--- Write the file to the mylogs directory --->
    <cffile action="write"
     file="c:/mylogs/#filename#"
     output="<html><body>#tempError#</body></html>">

  </cfcatch>
</cftry>

The <cfsavecontent> tag will save and record into a variable any code that is executed within it. In this case, we'll execute the <cfdump> tag so that the output is saved into the tempError variable. Then we set up a filename that is simply the date/time of the error (including milliseconds to avoid filename conflicts if errors occur at the same time.) Then we simply write the file to the c:/mylogs directory. Make sure the directory exists or you will get an error.

Even More Detailed Error Messages

If you combine this detailed error message with the debugging information I demonstrated in the article Better Debug Info for ColdFusion Development (http://www.communitymx.com/abstract.cfm?cid=ED0D6), you can get some really detailed error logs.

The code shown in the article will dump all the variables available at the time the error occurred. We'll modify the code shown in the File section above to include the variable dump within the <cfsavecontent> tag:

<cftry>
  <cfquery name="rsBlah" datasource="MyDSN">
   SELECT * FROM MyTable WHERE MyID = #id#
  </cfquery>

  <cfcatch>
    <cfsavecontent variable="tempError">

<!--- Dump all variables --->
<div>
<h1>Session</h1>
<cfdump var=#session# />
<br><br>
<h1>Application</h1>
<cfdump var=#application# />
<br><br>
<h1>Form</h1>
<cfdump var=#form# />
<br><br>
<h1>URL</h1>
<cfdump var=#url# />
<br><br>
<h1>Request</h1>
<cfdump var=#request# />
<br><br>
<h1>Cookies</h1>
<cfdump var=#cookie# />
<br><br>
<h1>CGI</h1>
<cfdump var=#CGI# />
<br><br>
<h1>Server</h1>
<cfdump var=#Server# />
<br><br>
<h1>Variables</h1>
<cfdump var=#variables# />
<br><br>
</div>
<!--- End variable dump --->

<!--- Dump the cfcatch struct --->
      <cfdump var=#cfcatch#>

    </cfsavecontent>
    <cfset filename="#DateFormat(Now(),'MMDDYYYY')#-#TimeFormat(Now(),'hhmmssl')#.html">
    <cffile action="write"
     file="c:/mylogs/#filename#"
     output="<html><body>#tempError#</body></html>">
  </cfcatch>
</cftry>

Of course, you can use any or all of the variable structures needed for your particular application. The full boat of variable structures will generate a huge file. For example, the CGI collection will give you a list of over 40 variables -- whether they have something in them or not. The Server collection will give you a list of all CFCS available. Use whichever variable collections you feel would be of use in your debugging endeavors.

Conclusion

A simple cftry/cfcatch block can give you a detailed error message using the built-in structure called cfcatch. The information can aid you in fixing errors in your applications as you develop them.