Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
I am the chief technical officer at InVision App, Inc - a prototyping and collaboration platform for designers, built by designers. I also rock out in JavaScript and ColdFusion 24x7.
Meanwhile on Twitter
Loading latest tweet...
Ben Nadel at the jQuery Conference 2009 (Cambridge, MA) with:

Using The OnError() Event Handler Implies A "200 OK" Response Status Code

By Ben Nadel on
Tags: ColdFusion

Special thanks goes to David McGuigan for pointing this out to me and I am just passing it on. This is just a minor note for anyone who perhaps hadn't thought about it (just as I had not thought about it); when you use the OnError() event handler in ColdFusion's Application.cfc, a "200 OK" status code response is implied. By that, I mean to say that a page that would normally respond with a "500 Server Error" status code will respond with a "200 OK" status code if that raised exception is caught by the OnError() event handler.

You can, of course, override the status code from within the OnError() event handler; but the overriding value would be something like a 404 or a 401 error - you will never have to override with a 200. When you think about error handling and picture the OnError() method as the root-most "Try/Catch" block, then this makes sense. For some reason, however, I just always assumed that OnError() would create a 500 status code, but allow you to handle it gracefully.

Anyway, thanks David for teaching me some good stuff!



Reader Comments

Overriding the response code is something I do when I'm making AJAX requests. I find it far easier to create a callback method that responds to a 500 response than to try to tell the difference between an actual "200 OK" response and an error that's been given a "200 OK" response code.

$.02

Reply to this Comment

@Matt,

I have nothing against overriding a response code. I'm only saying that when you use the OnError() event handler, it doesn't use a 500 error code for the error - it uses the 200. So, if you want a 500, then you have to do that manually.

Reply to this Comment

@Matt,

One thing to keep in mind is that by fudging a 500 error, you are adding a 500 error to your logs. Which means any web site reporting tool such as Urchin will report these as errors within your sites. I have been on many a contract where monitoring and reducing the number of 500 errors is a requirement.

To that end, Ben's method of passing back a response struct for both successes or failures when using AJAX is a much better solution in that it allows us to process the errors just as easily (a simple IF statement in the success callback checking the flag), and doesn't artificially mess with the web site logs.

Reply to this Comment

@Ben

Didn't mean to make it sound like you were saying not to. Just pointing out how I've used that in the past.

@Rob

True, but in this case that's not so much a problem. By the time the error hits the point where I override the response code, something has gone terribly wrong and I want to know about it. Overriding the status code makes it easier for me to detect these errors and allow the javascript to let the user know that something unexpected has happened.

Reply to this Comment

Hi Ben,

Good blog.

Question: Sorry it maybe off topic a bit. Is it possible to override onError() method? I am looking for a solution to pass additional arguments to this method if error happens during request processing. Simply speaking, if I have data submitted from the form and I am processing this data, I want to use something like onError(form) and dump that form on the error.cfm page if error happens during processing.

Andrey

Reply to this Comment

@Andrey,

You can override the signature of any method in ColdFusion (that you defined) because there are no strict signatures. So, you could add additional 3rd, 4th, etc. arguments to the onError() method (the first two are provided by CF - Exception object, Event). Of course, the tricky thing is, how do you call that method.

What you'd have to do is wrap your "dangerous" code in a Try/Catch block and then, inside your CFCatch, you'd have to invoke the onError() method explicitly.

I'll see if I can put some code together to show you what I mean.

Reply to this Comment

Post A Comment

You — Get Out Of My Dreams, Get Into My Comments
Live in the Now
Oops!
Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.