Skip to main content
Ben Nadel at CFUNITED 2008 (Washington, D.C.) with: Elliott Sprehn
Ben Nadel at CFUNITED 2008 (Washington, D.C.) with: Elliott Sprehn ( @ElliottZ )

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

By on
Tags:

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

26 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

15,663 Comments

@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.

11 Comments

@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.

26 Comments

@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.

1 Comments

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

15,663 Comments

@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.

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel