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

Posted April 29, 2009 at 9:16 AM

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!

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page




Reader Comments

Apr 29, 2009 at 9:40 AM // reply »
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


Apr 29, 2009 at 9:42 AM // reply »
7,572 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.


Apr 29, 2009 at 11:05 AM // reply »
7 Comments

Good to know, especially if monitoring a site.


Rob
Apr 29, 2009 at 11:24 AM // reply »
8 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.


Apr 29, 2009 at 1:58 PM // reply »
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.


Jan 29, 2010 at 4:02 PM // reply »
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


Jan 29, 2010 at 4:43 PM // reply »
7,572 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.


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 21, 2010 at 6:32 AM
ColdFusion CFPOP - My First Look
Apologies... The field name in the db for C. is "BounceCode" It stores the code / message which is returned in the email. Sorry for the confusion. ... read »
Mar 21, 2010 at 6:29 AM
ColdFusion CFPOP - My First Look
@Jose Galdamez, Hi Ben and Jose 1st of all.. big thanks to Jose for his Skype chat a few weeks back. Your time was much appreciated. I have come up with a rather unelegant solution to my problem a ... read »
Mar 21, 2010 at 3:42 AM
A New Wrist Pain
Chiropractic treatment is one of the best methods for treating numerous health problems naturally. After years of experience being a chiropractor, I have found that it is a powerful way to solve many ... read »
Mar 20, 2010 at 12:07 PM
Drawing On The iPhone Canvas With jQuery And ColdFusion
Simply awesome. Saved my day. ... read »
Mar 20, 2010 at 9:00 AM
Building A Fixed-Position Bottom Menu Bar (ala FaceBook)
I would like to say thx for an easy way to create a bottom bar. I do have a ?. Is it possible to center the bar if i want to resize it to ex 85%. Regards Offenbach ... read »
Mar 19, 2010 at 7:26 PM
MySQL 3/4 - com.mysql.jdbc.Driver And allowMultiQueries=true
Thank you very much for this post. Adding allowMultiQueries="true" in context.xml didn't help until I added it to url as allowMultiQueries=true Good idea is to use prepared statements and it will he ... read »
Jim
Mar 19, 2010 at 4:49 PM
Nobody Puts Baby In The Corner!
Wow. This is like suddenly finding a support group for your secret shame. I'm not alone! I always liked this movie, even though it is extremely cheesy. I just wish Jennifer Grey hadn't gotten the ... read »
Mar 19, 2010 at 4:47 PM
Application.cfc OnRequest() Method Affects OnError() Arguments
@Jason and @Ben, I've been doing some CF9 refactoring on our systems and noticed an odd occurrence with onError as well. Found a way to work around my problem, but what I saw was... Background: Our ... read »