Cfheader question.

248 views
Skip to first unread message

Serge Droganov

unread,
Jul 22, 2012, 9:36:40 AM7/22/12
to ra...@googlegroups.com
Hello,
I would like to use http status codes with custom status text, but when I do:

header statuscode=500 statustext="Foo";

I can see that browser gets:
HTTP/1.1 500 Internal Server Error

Is that a must or a bug? I'd really much love to use status text for messaging to my ajax proxy.

Thank you,
Serge

Peter Boughton

unread,
Jul 22, 2012, 1:56:54 PM7/22/12
to ra...@googlegroups.com
That works fine for me, so two things to check:

1) Make sure your browser is not masking the custom text (old versions of IE used to do this - not sure if recent ones do or not?)

2) Make sure that line is the one being called and sending the 500, rather than another unhandled error causing it.

Serge Droganov

unread,
Jul 22, 2012, 2:08:04 PM7/22/12
to ra...@googlegroups.com
Peter, I checked with Chrome and FF:
<cfheader statuscode="500" statustext="Foo"/>

Gives me:
HTTP/1.1 500 Internal Server Error
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=UTF-8
Content-Length: 6431
Date: Sun, 22 Jul 2012 18:04:07 GMT
Connection: close

It was direct call to Tomcat, no proxies or so on.

Of course if I take off cfheader I get 200 OK, so no other exceptions fired.
I'm not sure if browser can mask statustext, but if so, I have to find some other solution, because I can't control every browser settings.

Peter Boughton

unread,
Jul 22, 2012, 2:27:46 PM7/22/12
to ra...@googlegroups.com
That's not the code you posted before? :/

Anyhow, I tested this:

    <cfscript>
    header statuscode=500 statustext="Foo";
    </cfscript>

And this:


    <cfheader statuscode="500" statustext="Foo"/>


And for both of them I get expected response headers:

    HTTP/1.1 500 Foo

    Date: Sun, 22 Jul 2012 18:15:07 GMT
    Content-Type: text/html;charset=UTF-8
    Content-Length: 2
    Server: Jetty(8.1.2.v20120308)

That was with Railo 4 beta, and I also checked v3.3.1

If I generate an unhandled error, I don't get it, I get this:

    HTTP/1.1 500 Server Error
    Date: Sun, 22 Jul 2012 18:16:25 GMT
    Content-Type: text/html;charset=UTF-8
    Content-Length: 6560
    Server: Jetty(8.1.2.v20120308)


Given that the wording was different to yours, that made me wonder if Tomcat might be doing something different to Jetty, so I tested against Tomcat 6 / Railo 3.3.1 and I get:


    HTTP/1.1 500 Internal Server Error
    Server: Apache-Coyote/1.1
    Content-Type: text/html;charset=UTF-8
    Content-Length: 2
    Date: Sun, 22 Jul 2012 18:20:17 GMT
    Connection: close

and


    HTTP/1.1 500 Internal Server Error
    Server: Apache-Coyote/1.1
    Content-Type: text/html;charset=UTF-8
    Content-Length: 5176
    Date: Sun, 22 Jul 2012 18:20:29 GMT
    Connection: close


So, there we go - Tomcat is masking the status text.

Not sure if that's a Tomcat bug or if there's a config option for it somewhere...

Serge Droganov

unread,
Jul 22, 2012, 2:57:10 PM7/22/12
to ra...@googlegroups.com
I guess we just can't use any status code or statusText that we want to, it most likely not allowed with HTTP specification.

It would be great (restful) to handle ajax forms with custom statusText values, but I suspect we'll have to write it to response body.

Peter Boughton

unread,
Jul 22, 2012, 3:06:04 PM7/22/12
to ra...@googlegroups.com
I don't know if the specification allows/forbids changing the text, but in any case I guess it depends on what you're trying to do - you can always send an X-Custom-Header if you want to communicate a particular message with the client.

Also, don't forget an application-level error is different to a server error. If you're catching the error it possibly indicates it should receive a 4xx response instead?

Serge Droganov

unread,
Jul 22, 2012, 3:26:45 PM7/22/12
to ra...@googlegroups.com
That's what what I want to do on a client side (j is jQuery):

j.post(form.attr("action"), form.serialize())
.success(function(){
form.hide();
success.show();
})
.error(function(data){
error.text(data.statusText).fadeIn("fast");
})
.complete(function(){
submit.removeClass('busy');
});

So the idea was to use statusText for messaging/indication.

If controller works as:

try {
id_item = e.t.getBean("item").create(form, "sanitize");
}
catch (any error) {
        e.t.getService("log").exception(error);
e.t.header(
Val(error.code) ? error.code : 500, error.message
);
abort;
}

I can use cfthrow to communicate user if anything goes wrong, and it would be handled as any other exception. I like this approach a lot, but it's not a big deal if we print message to the response body, just a couple of code lines would be added to js error handler in this case:

error(function(data){
try {
error.text(j.parseJSON(data.responseText)).fadeIn("fast");
}
catch (e) {
error.text("Error " + data.status + " (" + data.statusText + ")").fadeIn("fast");
}
})

So, with this setup we're able to catch any non-20x-coded situation, and also debug all exceptions that out server gets.

Paul Cormier

unread,
Nov 18, 2014, 5:52:08 PM11/18/14
to ra...@googlegroups.com
I'm experiencing this very same issue where cfheader or cfscript header statustext is not making it to the client. This very much looks like a Tomcat/nginx issue. Has anyone figured out how to passthrough custom statustext?

Paul

Jochem van Dieten

unread,
Nov 18, 2014, 7:37:50 PM11/18/14
to ra...@googlegroups.com
On Tue, Nov 18, 2014 at 11:52 PM, Paul Cormier wrote:
I'm experiencing this very same issue where cfheader or cfscript header statustext is not making it to the client. This very much looks like a Tomcat/nginx issue.

The HTTP RFC specifically says you are allowed to replace the "reason phrase" with whatever local equivalent you want since it is intended for human consumption (as opposed to the "status code" which is for the application). However, even if you put an error in the reason phrase for a 500 response, the RFC says you still should put it in the body as well.

Anyhow, for Tomcat look into the USE_CUSTOM_STATUS_MSG_IN_HEADER property. 

Jochem


--

Reply all
Reply to author
Forward
0 new messages