FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Custom error-handling creates "500 internal server error"
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Custom error-handling creates "500 internal server error" [message #174400] Fri, 10 June 2011 08:18 Go to next message
Luke is currently offline  Luke
Messages: 10
Registered: June 2011
Karma: 0
Junior Member
Hi there,

For AJAX-calls I'm buffering my output, check for errors and if an error occurred print a custom, json-formatted error message. If everything works as expected, the buffered output is printed. I don't send any headers in my script, but for some reason if an error occurs and my buffering's on, I get a "500 (Internal Server Error)" as response-header. If I switch off my custom error-handling an the same error is thrown, PHP responds with a "200 OK".. How can that be?

My code's this:

// Suppress normal error output
ini_set('display_errors', 0);

// buffer Output
ob_start();

// When the PHP-Script's ended check for errors an print buffered content or an error message
register_shutdown_function(function() {

$error = error_get_last();

if($error) {
ob_end_clean();
header('Content-type: application/json');
echo "{'some': 'json here'}";
}else{
ob_end_flush();
}

});
Re: Custom error-handling creates "500 internal server error" [message #174402 is a reply to message #174400] Fri, 10 June 2011 09:24 Go to previous messageGo to next message
Erwin Moller is currently offline  Erwin Moller
Messages: 228
Registered: September 2010
Karma: 0
Senior Member
On 6/10/2011 10:18 AM, Luke wrote:
> Hi there,
>
> For AJAX-calls I'm buffering my output, check for errors and if an error occurred print a custom, json-formatted error message. If everything works as expected, the buffered output is printed. I don't send any headers in my script, but for some reason if an error occurs and my buffering's on, I get a "500 (Internal Server Error)" as response-header. If I switch off my custom error-handling an the same error is thrown, PHP responds with a "200 OK". How can that be?
>
> My code's this:
>
> // Suppress normal error output
> ini_set('display_errors', 0);
>
> // buffer Output
> ob_start();
>
> // When the PHP-Script's ended check for errors an print buffered content or an error message
> register_shutdown_function(function() {
>
> $error = error_get_last();
>
> if($error) {
> ob_end_clean();
> header('Content-type: application/json');
> echo "{'some': 'json here'}";
> }else{
> ob_end_flush();
> }
>
> });

http://nl2.php.net/manual/en/function.register-shutdown-function.php

Read it. :-)

Hint: Look up 'function' explanation:


=============================================
The shutdown function to register.

The shutdown functions are called as the part of the request so that
it's possible to send the output from them. There is currently no way to
process the data with output buffering functions in the shutdown function.
=============================================

Regards,
Erwin Moller

--
"That which can be asserted without evidence, can be dismissed without
evidence."
-- Christopher Hitchens
Re: Custom error-handling creates "500 internal server error" [message #174403 is a reply to message #174402] Fri, 10 June 2011 09:57 Go to previous messageGo to next message
Luke is currently offline  Luke
Messages: 10
Registered: June 2011
Karma: 0
Junior Member
I'm sorry I don't get it. Does that mean I cannot use ob_xx functions in the register_shutdown_function callback? Because it *does* work, except it sends a 500 header. Also, if there aren't any errors and ob_end_flush(); is called, a 200 OK header is sent.
Re: Custom error-handling creates "500 internal server error" [message #174406 is a reply to message #174403] Fri, 10 June 2011 11:22 Go to previous messageGo to next message
Erwin Moller is currently offline  Erwin Moller
Messages: 228
Registered: September 2010
Karma: 0
Senior Member
On 6/10/2011 11:57 AM, Luke wrote:
> I'm sorry I don't get it. Does that mean I cannot use ob_xx functions in the register_shutdown_function callback? Because it *does* work, except it sends a 500 header. Also, if there aren't any errors and ob_end_flush(); is called, a 200 OK header is sent.

(Please stop using Google groups latest greatest interface to usenet.
Your answers appears as a fresh posting to many regular usenet (as in
n-=gg). Not in theright thread, but as a new top posting. I understand
that the former version does it right.)

To your question:
I think the manual is very clear:
"The shutdown functions are called as the part of the request so that
it's possible to send the output from them. There is currently no way to
process the data with output buffering functions in the shutdown function. "

So no, you cannot use your output buffering.

It is hard for me to help you with the 200 and 500 headers without
seeing the whole set-up: Where do the possible errors arise?
Are you using custum errorhandler routines? If so, how do they look?
Are you handling only errors or also exceptions?
And many more questions. :-)

However, I can tell you how I debug in difficult situations (eg, INSIDE
the errorhandler): create a simple function that appends to a file, or
sends you an email. Add a timestamp to the message.
something like:
function debug($someMessage){
// add timestamp and email to yourself, or append to some file.
}

Then, from places that are otherwise hard to debug, use that function.
I wasted many days before I started using that approach. ;-)

Sorry I cannot be of more help. Maybe somebody else can.
Good luck.

Regards,
Erwin Moller


--
"That which can be asserted without evidence, can be dismissed without
evidence."
-- Christopher Hitchens
Re: Custom error-handling creates "500 internal server error" [message #174408 is a reply to message #174406] Fri, 10 June 2011 12:30 Go to previous message
Luke is currently offline  Luke
Messages: 10
Registered: June 2011
Karma: 0
Junior Member
On Jun 10, 1:22 pm, Erwin Moller
<Since_humans_read_this_I_am_spammed_too_m...@spamyourself.com> wrote:
> (Please stop using Google groups latest greatest interface to usenet.
> Your answers appears as a fresh posting to many regular usenet (as in
> n-=gg). Not in theright thread, but as a new top posting. I understand
> that the former version does it right.)

Ok, using the old Google Groups now.


> It is hard for me to help you with the 200 and 500 headers without
> seeing the whole set-up: Where do the possible errors arise?

I found out the problem is this: I'm buffering the output to print one
(JSON-formatted) error message without any other text, no matter how
many errors occurred. That way I can return a valid JSON-Message to
AJAX-calls. If I relied on PHP's error-handling it would print error-
message anywhere in the code, so I wouldn't be able to send a valid
JSON-string. To do that I call

ini_set('display_errors', 0);

Now if ini_set('display_errors', 0); is set to suppress error-output
and an error occurs a 500 header is sent. I found out writing

error_reporting(0);

instead also suppresses error-output but does not send a 500-header if
an error occurs. That fixed it.

Still, thanks for your help.

Lukas
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Install GD on Windows
Next Topic: FDF extensions to PHP will not compile
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Fri Sep 20 15:37:27 GMT 2024

Total time taken to generate the page: 0.02400 seconds