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!
Looking For A New Job?
- Senior Coldfusion Developer - Remote Position at MeetingPlay
- Senior ColdFusion / Mura Developer at Fig Leaf Software
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.
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.
Good to know, especially if monitoring a site.
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.
Didn't mean to make it sound like you were saying not to. Just pointing out how I've used that in the past.
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.
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.