The World's Favorite Open Source GPL J2EE CFML Runtime Engine

BlueDragon Developer's Journal

Subscribe to BlueDragon Developer's Journal: eMailAlertsEmail Alerts newslettersWeekly Newsletters
Get BlueDragon Developer's Journal: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn


Open BlueDragon Authors: Reuven Cohen, Elizabeth White, Michael Sheehan, John Gauntt, Salvatore Genovese

Related Topics: Open BlueDragon Developer's Journal, ColdFusion on Ulitzer

BlueDragon: Article

CFDJ Feature - How to Debug Your ColdFusion Applications

Problem solving and techniques for your debugging adventures

I just spent four hours debugging an error for a client. The client is an application service provider, and they developed an administrator for internal use. The administrator allows for the user to switch between sites, at whim. The "site switcher" is a drop menu, which loads up a different DNS depending on the selected site.

A certain set of code was working for all sites except one. The application was not throwing errors, but nothing was getting displayed on the page. How do you find the problem? I'm going to step you through the process I took to solve the problem, and along the way, I hope you'll learn a technique or two that you can use in your own debugging adventures.

Two Types of Errors
There are really two types of errors that you'll encounter in your code: syntax errors and logic errors. Syntax errors are those ugly ColdFusion errors that you are used to seeing every day as you write code (I know I'm used to seeing them). Generally these are easy to discover and address. These are usually squashed during your development.

The most common reason why I see syntax errors on production sites is due to undefined variables being accessed. Sometimes there is some link somewhere that is missing a URL variable, or perhaps someone changed the Query String and reloaded a page. Sometimes a checkbox form element is accessed, yet the user did not check anything before submitting the form, so it is undefined or a shared scope variable may be accessed before it is set. Other reasons that ColdFusion might throw syntax errors might be invalid tags or attributes, mismatched tags, or database timeout.

Syntax errors are easier to find than logic errors. Logic errors occur when the code that you've written doesn't satisfy the business requirements you were setting out to implement. Some business requirements might be easy to fix, such as display 20 products per page instead of 15. Others might become murky to catch, such as people who buy at least five products from Product Line A get a 10 percent discount on all products from Product Line A in their order, unless they have three products of Product Line B attached to the same order, whereas they receive the bigger discount of 10 percent off Product Line A products or 5 percent off of all Product Line B products. The best way to avoid business logic errors like these is to have a clear set of business requirements before you start to code the application. Sometimes it's hard for the client to see the big picture.

Examining the Debugging Output
Going back to my problem of the day, I started out by assuming that the code was fine, since it was working for all of the sites except one. The problem must reside in the data. The page in question was supposed to show a list programs from the database. If no programs existed, then a blank display is not a surprise. I tracked down the query code and executed it manually against the database using Query Analyzer (an SQL Server tool). The query worked fine and the proper data was returned. This query didn't contain any CF variables, so the cause of this couldn't have been invalid values.

Just to be sure, I wanted to review the SQL actually being sent to the DB. For this I needed to turn on ColdFusion's debugging output. In normal cases, you wouldn't turn on debugging for a production site, but I decided this case was an exception. The site only has half a dozen users, none of whom could use the site until the error was fixed. You can turn on debugging from your ColdFusion Administrator using these instructions:

1.  Go to your administrator. Most likely the URL is www.yoursite.com/cfide/administrator. You'll see a screen similar to Figure 1. It might look a little different for those who have already upgraded to CF7. Enter your password to login.

2.  Under Debugging and Logging select Debugging Settings. You'll see the different things that can be displayed in debugging output, as shown in Figure 2.

3.  Check the Enable Debugging box, if not already done. I also like to click the Enable Robust Exception Information so that I can view the full SQL statement, and other information about the queries that run.

4.  While you're in here, take some time to examine the list of other information that can be displayed in the debugging output. You can select the type of display, either dockable or classic. I prefer classic, the default method. You can also display various variable scopes, tracing information (used in conjunction with the cftrace tag), exception information, database activity, and general debug information. Most commonly I use the variables and database activity.

Back to the problem at hand: I enabled debugging and reran the template. I scrolled down and looked for the debugging output. The query I was looking for was not executed. Why was that?

cfdump and cfabort
The template in question is what I call a "case" template. Based on some mode variable, different code is executed within the template, often with completely different execution and results. I call it a case template because a different cfcase statement represents each mode. All of this inside the cfswitch tag. For more information, see: http://livedocs.macromedia.com/coldfusion/7/ htmldocs/00000339.htm#1103819. This type of template is very similar to a fusebox approach to designing Web applications. The mode would be a "fuseaction" and the base template would be the "fusebox" template. I know by looking at the URL that a client-side redirect is not happening, which leaves two options. Either the wrong "mode" is being executed or a server side redirect is happening.

The first thing I wanted to do was to verify that the mode was correct. I turn to cfdump tag for that. cfdump is a very useful debugging tag that displays the contents of a variable. With simple variables, you could use a cfoutput to display the value of that variable, however for complex variables such as an array, structure, or XML document the cfdump is invaluable. cfdump has a few attributes, but the one we care about for the moment is the var attribute. You use the var attribute to specify which variable you want to display. Always make sure to enclose the variable name in pound signs, or else the variable name will be displayed instead of the variable value. You can check out the full documentation on cfdump at http://livedocs.macromedia.com/coldfusion/7/ htmldocs/00000239.htm#3765824.

I used cfdump to dump the value of the mode variable before entering the cfswitch tag. The code looked something like this:

<!--- code here --->
<cfdump var="#mode#">

<cfswitch expression="#mode#">
<!--- more code here --->

More Stories By Jeffry Houser

Jeffry is a technical entrepreneur with over 10 years of making the web work for you. Lately Jeffry has been cooped up in his cave building the first in a line of easy to use interface components for Flex Developers at www.flextras.com . He has a Computer Science degree from the days before business met the Internet and owns DotComIt, an Adobe Solutions Partner specializing in Rich Internet Applications. Jeffry is an Adobe Community Expert and produces The Flex Show, a podcast that includes expert interviews and screencast tutorials. Jeffry is also co-manager of the Hartford CT Adobe User Group, author of three ColdFusion books and over 30 articles, and has spoken at various events all over the US. In his spare time he is a musician, old school adventure game aficionado, and recording engineer. He also owns a Wii. You can read his blog at www.jeffryhouser.com, check out his podcast at www.theflexshow.com or check out his company at www.dot-com-it.com.

Comments (1) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
CFDJ News Desk 10/21/05 09:26:15 PM EDT

CFDJ Feature - How to Debug Your ColdFusion Applications. I just spent four hours debugging an error for a client. The client is an application service provider, and they developed an administrator for internal use. The administrator allows for the user to switch between sites, at whim. The 'site switcher' is a drop menu, which loads up a different DNS depending on the selected site.