Snippets

Visser Labs Intercepting fatal PHP errors as they happen

Created by Michael Visser
<?php

function custom_init() {

    add_action( 'shutdown', 'custom_fatal_error_interceptor' );

    // Do something that might cause a fatal error
    maybe_fail_whale();

    remove_action( 'shutdown', 'custom_fatal_error_interceptor' );

}
add_action( 'init', 'custom_init' );

function custom_fatal_error_interceptor() {

    // Get the last PHP error details
	$error = error_get_last();
	if( $error !== null ) {

        if ( substr( $error['message'], 0, 22 ) === 'Maximum execution time' ) {

			// Friendly e-mail to WordPress site administrator about long processes

        } elseif ( substr( $error['message'], 0, 19 ) === 'Allowed memory size' ) {

			// Friendly e-mail about memory restriction

        } else if( $error['type'] === E_ERROR ) {

			// Check for other types of fatal errors

            if( substr( $error['message'], 0, 36 ) == "Cannot access protected property WC_" ) {

                // Friendly e-mail about WooCommerce 3.0 incompatibility

            } else {

            	// Check for known Plugin conflicts...

            	// Check for missing Plugin resources...

            	// Check for common issues where the reason for the fatal error can be established from the error message provided to us by error_get_last()

            }

		}

    }

}

?>

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.