Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling PHP / mysql errors (including fatal) via custom error page

What is the best way to handle errors on front end websites? Similar to twitter, reddit, etc that have a nice 'something went wrong' page. I want to:

  1. detect errors and be notified, even if its just to a log file
  2. show a custom error page such as the fail whale or similar, so its obvious something has gone wrong instead of showing nothing, or a horrible looking error.

Ideally anything that prevents a user from seeing the site, like fatal errors, custom exceptions, mysql being down, random 500 errors etc, should show the custom error page.

Fatal Errors

PHP : Custom error handler - handling parse & fatal errors - works ok, but if this happens at the end of a script, and it has already output something, this wont work

Non Fatal Errors

http://davidwalsh.name/custom-error-handling-php

MySQL Errors

Ideally need to cover everything from a simple query error, to the server not being there. Imagine its as simple as detecting the error in my database class and calling redirecting to / calling the error method

Server Errors

Is this something I should be doing in PHP OR Apache OR both?

Code at the moment

Currently I have the following from a mix of various other SO's, but fatal errors will just be reported after the rest of the page has loaded.

Anyone have any ideas for something that will work with all of the above?

<?
function xhandler($number,$string,$file,$line,$context)
{
    //log to text file?
    //log to xml file?
    //store in database?
    //whatever you want to do!

    echo "<h1>here be dragons!</h1>";
    echo "$number,$string,$file,$line,$context";
    exit();
}

function fatal_handler() {
    $errfile = "unknown file";
    $errstr  = "shutdown";
    $errno   = E_CORE_ERROR;
    $errline = 0;

    $error = error_get_last();

    if( $error !== NULL) {
        $errno   = $error["type"];
        $errfile = $error["file"];
        $errline = $error["line"];
        $errstr  = $error["message"];
    }

    echo "<h1>here be dragons!</h1>";
    print_r($error);
    exit();
}

register_shutdown_function( "fatal_handler" );
set_error_handler('xhandler',E_ALL);

include('show_the_content');

function_that_doesnt_exist();
echo $unterminated_line_of_code
?>
like image 632
Horse Avatar asked Nov 11 '22 18:11

Horse


1 Answers

You can't catch fatal errors. They are fatal, script execution stops immediately when they're encountered. These errors should never appear on production sites.

Notices and warnings should be suppressed from showing in HTML on production environment (live sites).

What these sites usually do is handle HTTP errors with PHP. You need to redirect these errors to a PHP script which will handle them. For example, in Apache you do it with the ErrorDocument directive. You can put these directives (one for each HTTP error code) in the server configuration or in the .htaccess file in the web site's document root.

In the PHP script, you can filter different HTTP errors with $_SERVER['REDIRECT_STATUS'], if you need to. When you receive a request with the desired HTTP error, you can handle it any way you wish.

like image 54
morgoth84 Avatar answered Nov 14 '22 21:11

morgoth84