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

Posted April 29, 2009 at 9:16 AM by Ben Nadel

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

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 »
11,238 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.


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


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 »
11,238 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 A Comment

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.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 22, 2013 at 4:43 AM
How Do You Use The ColdFusion CFParam Tag?
'<cfparam>' or 'isDefined()and <cfset>' performs the same task.Is there any difference? ... read »
May 21, 2013 at 7:46 PM
Using Plupload For Drag & Drop File Uploads In ColdFusion
No luck. At least I have uncovered the cause, URLScan 3.1. Here is what I see in the IIS log when a file is over 30mb. 2013-05-21 23:29:05 10.105.45.128 GET /plupload/assets/jquery/jquery-1.8. ... read »
May 21, 2013 at 6:12 PM
Using Plupload For Drag & Drop File Uploads In ColdFusion
Ben, I did not see you after Pete Freitag's Lockdown session at cfObjective but he said that IIS sets file size limits at 30MB by default which just happened to be the threshold for file size when ... read »
May 21, 2013 at 11:51 AM
Ask Ben: Parsing Very Large XML Documents In ColdFusion
Looking at my first ever XML document that I have to parse and put into MS SQL 2000 with CF8. I get it to list the desired Field name, many times over, and have a long list of this field name displa ... read »
May 21, 2013 at 9:25 AM
Turning Off and On Identity Column in SQL Server
you are awesome..i am lucky to get this blog between such a garbage one....Thanks, Prashant ... read »
May 20, 2013 at 4:38 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Dana, Your confusion is well founded, since this is a very confusing features. In fact, it ONLY works if you use array notation. Meaning, that this: arrayToList( query[ "columnName" ] ) ... read »
May 20, 2013 at 4:34 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
I was thinking chicken and the egg, I wouldn't have expected it to work in the valuelist going in I guess. Maybe I just need a beer, long day :) ... read »
May 20, 2013 at 4:29 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Dana, That's if you're trying to reference a specific row. In this case, we're trying to reference the entire query column as one cohesive value. So, you are correct that if you wanted to output a ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools