Graceful recovery

Right now some situations (parse errors, undefined function call, no more memory) in PHP result in fatal error – which means the engine can not continue with the request beyond this point. From the user point of view, this often results in a blank page. I wonder if it would be possible to have standard recovery mechanism that would allow the PHP engine on fatal error to enter some kind of “recovery mode” when it would output some very basic page saying “ok, I have some problems here, but don’t panic and just tell my programmer to fix the code”. It won’t give much info probably but it would allow production sites display nice message to the users instead of the boring snowfield panorama it displays now (that is if the administrator was smart enough to set display_errors to off).

Maybe it should allow only fixed HTML, or maybe some kind of “request recovery” mode which would create some “recovery mode” sub-request when it would allow to do more – like send emails to webmaster🙂. This may need some creative thinking but the main idea is to move away from the snowfield thing.

14 thoughts on “Graceful recovery

  1. @Boy: Hahahahahaha! You = funny! I guess you never worked in the ever changing landscape of very large and complex corporate web aplications. Some client wants something done ASAP, we code, we test, we put live are little gem before the requested deadline and what happens? All too often code breaks _somewhere_ because someone changes something without looking at other peoples code and fatal errors occur. If communication can fail then so can thorough testing. And this is not even walking the minefield of third party aplications/hardware updates/emergency server migration/the boss’ nephew who (for god knows what reason) is also allowed to muck about on the server and has the understanding of a gnat.

    No… I say that these functions are a VERY welcom adittion. Remember Finagle’s Law of Dynamic Negatives, dude!

  2. In my opinion this would be useless because people never put a site live/in production mode before thoroughly testing it, therefore if there are any fatal errors to be found, they are fixed before the site goes live. After a site is tested and proven to be bug free, only then people put it on live (unless they are dumb) therefore this feature (of displaying a nice error message) would be cool to have, but never really be used.

  3. Perfect! For those of you who doesn’t know, since PHP 5.2.0 There is now a function called “error_get_last()” which will return the last error. Which works in the shutdown function so not only is it possible to catch such errors, you can now also see the error, wonderful I say. This in turn also means that if you are running 5.2.0 or later you are no longer required to use a specific function to exit anymore as you could simply see if an E_ERROR occured.

  4. Nice timing!, I was just planning on implementing this today for Facebook code. We currently don’t display anything on fatal errors, and it’s prefered that the Platform and Web pages redirected the browser when anything bad happens. Although the output buffer solution is a good one, we already catch fatal errors in C so I was hoping to be able to send out a header (if not already sent) and redirect to a standard error page. We’ll see if it pans out🙂

  5. Better yet send a 500 Internal Server Error header instead!

    That’s what would be expected. It is then up to the webserver to display the 500 error page in a way that makes sense.

  6. hmm, I did wrap it with code tags… here is it again:

    ini_set( ‘memory_limit’, ‘100000’ );
    function shutdown()
    {
    $a = range(0, 45);
    echo “In shutdown\n”;
    }

    register_shutdown_function( ‘shutdown’ );
    $a = range(0, 4500);

  7. That’s an interesting idea, I wonder if it always works… But that would require special build of the page I guess, one that doesn’t output the data directly but stores it. Maybe with more buffering it can be made generic?

  8. I’ll try that again …

    Use ob_start() to cache your an error-page and if the script dies the cached
    content is sent to the browser:


    <?php

    ob_start();
    echo "<b>Sorry, this page is broken.</b>"; // error message

    $content = get_page_content(); // if this breaks, the above error is shown
    ob_end_clean();
    echo $content;
    ?>

  9. It does work, as long as you don’t use too much memory in the shutdown handler, see:

    However, when I change the range(0,45) to range(0,4500) it does not work any more (and no warning is shown again).

  10. Derick, nice, didn’t know that existed. However, I understand this is done using shutdown handlers? I’m not sure this would work in all situation – i.e., if you are out of memory it could be that shutdown handler would fail to work too.

Comments are closed.