Skip to content

Instantly share code, notes, and snippets.

@RadGH
Created August 26, 2020 06:29
Show Gist options
  • Save RadGH/b6ca63d2cf421f7233b5674c2c74bd05 to your computer and use it in GitHub Desktop.
Save RadGH/b6ca63d2cf421f7233b5674c2c74bd05 to your computer and use it in GitHub Desktop.
PHP Fatal Error Handler - Drop the file right into wp-content folder
<?php
/*
* A+A Error Reporter
* Created by [email protected]
*/
if ( !isset($error) || !isset($handled) ) {
echo 'Error: Unhandled exception occurred and required parameters $error and $handled were not supplied in '. __FILE__ .':'. __LINE__;
exit;
}
// Get error type as constant name instead of integer code
// based on https://www.php.net/manual/en/errorfunc.constants.php#109430
$error_type = "UNKNOWN_ERROR";
$types = 'E_ERROR,E_WARNING,E_PARSE,E_NOTICE,E_CORE_ERROR,E_CORE_WARNING,E_COMPILE_ERROR,E_COMPILE_WARNING,E_USER_ERROR,E_USER_WARNING,E_USER_NOTICE,E_STRICT,E_RECOVERABLE_ERROR,E_DEPRECATED,E_USER_DEPRECATED';
$types = explode(',', $types);
foreach( $types as $type ) if ( constant($type) == $error['type'] ) $error_type = $type;
// Get file path relative to document root
$path = str_replace( ABSPATH, '/', $error['file'] );
// Format the message
$message = '<h3>Error: ' . $error['message'] . '</h3>';
$message .= "\n\n<p><em>{$error_type} in {$path} on line {$error['line']}</em></p>";
// Begin outputting the error message
ob_start();
// Show a prettified error message
if ( function_exists('wpautop') ) {
echo wpautop($message);
}else{
echo nl2br($message);
}
// I don't know what this "handled" stuff is for so just dump it into a comment.
if ( !empty($handled) ) {
echo "<!-- Begin dump of \$handled\n";
echo 'Handled: <pre>';
var_dump($handled);
echo '</pre>';
echo "\nEnd of \$handled -->";
}
// Show how this message was generated
echo '<p><em>Error report generated by wp-content/php-error.php</em></p>';
$error_html = ob_get_clean();
$site_title = parse_url($_SERVER['REQUEST_URI'], PHP_URL_HOST);
if ( function_exists('get_bloginfo') ) $site_title = get_bloginfo('name');
$wp_error = new WP_Error(
'aa_fatal_error_handler',
$error_html,
array(
'error' => $error,
'handled' => $handled,
)
);
$title = 'Error on ' . $site_title;
wp_die( $wp_error, $title );
/**
* Format parameters based on type
* @param $args
*
* @return string
*/
function wperr_format_args( $args ) {
$str = array();
if ( !empty($args) ) foreach( $args as $value ) {
if ( is_null($value) ) $str[] = 'null';
else if ( is_float($value) ) $str[] = esc_html($value);
else if ( is_int($value) ) $str[] = esc_html($value);
else if ( is_bool($value) ) $str[] = $value ? 'true' : 'false';
else if ( is_array($value) ) $str[] = 'array( ' . json_encode( $value ) .' )';
else if ( is_object($value) ) $str[] = '(object '. get_class($value) .') ' . json_encode( $value );
else if ( is_string($value) ) $str[] = '"' . str_replace('"', '\"', esc_html($value)) . '"';
else $str[] = '(unknown type)'. json_encode( $value );
}
return implode( ', ', $str );
}
/**
* Gets a file path formatted relative to the document root
*
* @param $file
* @param $line
*
* @return string
*/
function wperr_format_path( $file, $line = false ) {
$file = str_replace( ABSPATH, '/', $file );
if ( $line ) {
$file = $file . ':' . $line;
}
return $file;
}
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment