Archive for the ‘Debugging’ Category

Logging complex data with Coldfusion and JSON

Tuesday, March 2nd, 2010

Logging with Coldfusion is very simple, but in and of itself a little limiting. Generally the log itself is a string. So while logs are certainly useful for recording an action and when it happened the amount of data that is presented in the log file is fairly restrictive without doing a whole lot of work, and even then your log file may look something like: “Error occured on page #thispage.cfm# by user #john smith#. The error catch message is #an error message#. It occurred on line #230#. I’ve wrapped the dynamic content with hashes. On top of all that we’re really not saying that much here. The thing is Coldfusion gives us a ton of data nicely organized as a struct, if only we could log it.

This is just another place where JSON becomes very handy for the developer.  By serializing the struct as JSON we can log the struct as a string.  If we need to see the struct we simply need to grab the JSON and deserialize it in which case the struct will be dumpable again.  Also for debugging purposes we developers often email ourselves errors when they occur.  This is a good practice, one that I do for all my applications.  Once an application goes into production the idea is that you shouldn’t be getting that many email errors, but during development (if you’re like me) you’ll get an error email about every 3 minutes.  As you can imagine emailing a struct is a significantly larger file size than sending that same struct as JSON.  I emailed the test struct below with coldfusion as both a struct and as JSON.  The struct html was 14.4 kb.  The JSON was 0.8 kb.  That means that I can make 18 more errors for every one error that I can make now before filling up the server.

Screen shot 2010-03-02 at 7.56.24 PM

Test Struct

I created the above struct because it contains the main data types.  You can see it is a struct that contains an array, an integer, another struct, a string, and a boolean.  The struct can be created with the following code.

<cfset var data = structnew()>
<cfset data.success = true>
<cfset data.message = "This is a message.">
<cfset data.anumber = 101>
<cfset data.anarray = arrayNew(1)>
<cfset data.astruct = structnew()>

<cfloop from="1" to="10" index="VARIABLES.i">
	<cfset arrayAppend(data.anarray, VARIABLES.i)>
	<cfset data.astruct[VARIABLES.i] = VARIABLES.i + 5>
</cfloop>

(more…)

Access denied for user ’someone’@'localhost’ (using password: YES)

Thursday, February 25th, 2010

If I had a nickel for every time I’ve gotten this error I’d be somewhere around 35 cents richer.  Point being this error really isn’t that big of a deal, but can certainly be somewhat annoying.  If you’re using shared web hosting and are receiving this error on your production site just call your host because there is probably nothing you can do on your end.

However, if you’re working on a VPS, dedicated server, the cloud, or any environment in which you have access to both the server and the Coldfusion Administrator then you’re in luck.  This error is usually thrown as a result of trying to access the database with a datasource set up for a user that doesn’t exist.  So if you’re using MySql on your server and you try to access it with an account that maybe exists on the local drive, but not on the server you’ll receive this error.  To test if this is the case go to the Coldfusion Administrator and verify the data source connection in question under the datasources menu.  It probably won’t verify.  At this point you will want to connect the datasource using a legitimate account or just use the ‘root’ username and no password.  This should verify the datasource and eliminate the error.

Firebug and Safari Web Inspector do not return errors when using onError method.

Thursday, February 25th, 2010

Just a quick development note.  This is obvious, but had me fooled for a little while.  Simply put coldfusion does not return an error via AJAX if the onError method is set in the Application.cfc file.  This is obvious because the error is being handled elsewhere.  Nonetheless, if you are in development mode using AJAX and have the onError method turned off then you can view your the errors that are thrown in the cfc being called remotely.  Very nice and very helpful.  Then you go into production, and, like any good developer you set up all of your error handling.  But.. But there is a problem with one of the ajax calls to a cfc.  No big thing, just inspect it in Firebug or Safari’s Web Inspector (or even IE8’s new developer tool if you can make it through all the security alerts).  But you don’t see an error, just an empty call.  Not only was an error not returned, nothing was returned.  Well, it’s just because your error handler template has done something else with the error.

So choose to tackle the error in whatever way seems fit.  But the console will not return Coldfusion/AJAX errors while the onError method is running.

Using Safari 4 to develop.

Thursday, December 17th, 2009

Like most developers I have always used firebug in Mozilla Firefox to debug web applications.  Why?  Because it’s an awesome tool.  However, this morning I thought for some reason that I should take a look at the development debugging tools that was included in Safari 4.  I’ve noticed that Safari 4 is recognizably faster than Firefox, and I’ve always found it difficult choosing a browser for testing.  The reason was that if I needed the console I felt I had to use Firefox, but if I just needed to check some functionality I preferred Safari for its speed.

So what was my revelation?  Simply put, Safari has a great built in developer tool.  It’s located under the ‘Develop’ menu at the top.  This menu is not enabled by default, so if you don’t see it go to Preferences > Advanced and check the bottom box labeled ‘Show Develop Menu in menu bar’.  Once you have your menu you’ll see your development tools.  The main one to focus on is the ‘Show Web Inspector’ item.  This is where the bulk of your development tools are located.  I’m not going to run through the features.  There are plenty of blogs out there that do this.

One thing though that I would like to mention, just because I had a little trouble finding it, is the console.  Unlike Firebug, it’s not located on the top menu bar.  Instead look for a small button in the lower left corner of the tool.  There are three buttons here, one of them opens the console.  It’s the one with a greater than sign and three horizontal lines.  Clicking this will open your console.  So if you run a cfc via cfajaxproxy your call to the method will show up here.  If you click it you will be taken to the resources panel where you can see your response and parameters and so forth.

Is it a better tool? Don’t know.  I just opened it this morning.  My guess is that like most applications there’s a time and place for both Firebug and the Safari Developer tool.

Last thing I’ll mention is that I like the design.  I stare at text files all day.  I think that getting to see all those nice colors and transparencies and rounded corners will help brighten my day a little.  Are they necessary for debugging?  Probably not.  But, hey, I’m only human.