Welcome!

ColdFusion Authors: Maureen O'Gara, Hovhannes Avoyan, Yakov Fain, Pat Romanski, Liz McMillan

Related Topics: ColdFusion

ColdFusion: Article

Macromedia ColdFusion Debugging - Don't Forget Your Bug Spray!

'Sorry about that Adam, we'll have to take a deeper look at it. Uh-huh. Yeah.

Since Adam's application is very complex, I have created a much smaller, simpler version of the problem, one you can look at, and, if so desire, easily implement in your own application.

This example randomly picks one of 10 phrases and reverses it. It's actually quite silly, much like all my bug jokes so far, but should give you a good idea of where I'm coming from.

Listing 1 shows the file the user will request. It's very straightforward and creates a small chain of function calls on line 4; it also instantiates the CFC shown in Listing 2.

Listing 2 is a component with two functions. The first, getPhrase(), will return a phrase based on the passed-in phraseNumber. If no phraseNumber is passed in, it will randomly pick a number. reversePhrase() asks getPhrase() for five random phrases and reverses and returns the fifth. The code of interest is the super.onCall() calls on lines 6 and 30, as well as the super.onResult() calls on lines 23 and 36. In this case "super" is a keyword that refers to Application.cfc (see Listing 3), since test.cfc extends Application.cfc (line 1, Listing 2).

Listing 3 is where the magic happens. When the user makes a request, based on the order of operations discussed before, the first bit of code ColdFusion processes is the onRequestStart() function. This creates an empty one-dimensional array called the cfcStack that will be accessed later in the request. Next index.cfm is run, which calls on my getPhrase() and reversePhrase() functions. Each of those functions call onCall() and onResult(), which log and stack what is going on in each function call. ColdFusion then processes the onRequestEnd() function, which checks to see if debugging is enabled and, if so, <cfdump>s the cfcStack.

This allows me to see each function that was called, in the order that it was called, what parameters it had to work with, and what results it returned. Notice the use of reserved functions described above onRequestStart() and onRequestEnd() as well as my own functions onCall() and onResult() within the Application.cfc.

I also added an onError() function, which in the case of an error would e-mail me the cfcStack as well as all the error information, and, if debugging is on, <cfrethrow> the error so I can see it immediately on the screen.

Just by taking a quick look at the cfcStack that is being dumped at the end of the request, I can tell if each function individually is behaving properly, as well as see if each one is fitting in with the parts of the whole request.

This simple <cfdump> is what led to this phone call: "Hey Adam, yeah, should be good to go. Alright man, thanks a lot. I hope you have a good weekend. Oh you're going there? Well have fun, and bring your bug spray; it can be pretty nasty this time of year."

More Stories By Nik Molnar

Nik Molnar is a ColdFusion/Flex developer with over seven years experience. He has led teams through the development of enterprise applications for the mortgage, sports ticketing, and stock industries. He is an amateur Iron Chef and posts regularly at his blog: foodDuo.com. He lives with his wife Katy and his dog Jacques in Orlando, Florida.

Comments (2) 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
Vijay Khambalkar 09/22/05 01:58:00 PM EDT

Thanks for the great article! I am stuck with a problem in cfthrow and would appreciate your help.

I did the onError handler. And want to throw another exception that will be caught in Flash. All the details are available at this stage, but cfthrow's message attribute does not take any variables.

E.g.
does not work. But works.

We want to use a variable in the message, otherwise there is no use of a common error handler.

Could you please advise?

TIA.

:Vijay

ColdFusion News Desk 09/14/05 10:10:35 PM EDT

Macromedia ColdFusion Debugging - Don't Forget Your Bug Spray! 'Sorry about that Adam, we'll have to take a deeper look at it. Uh-huh. Yeah. No, I totally understand. Okay then, I'll give you a call if I still can't replicate the problem.'