Skip to content

Instantly share code, notes, and snippets.

@Mte90

Mte90/snippet.php

Last active Dec 9, 2019
Embed
What would you like to do?
Custom WordPress Error Handler. Support Query Monitor and can be used as mu-plugin.
<?php
/*
* Plugin Name: Better errors
* Description: Better errors in log
* Author: Daniele Scasciafratte
* Version: 1.0
* Author URI: http://codeat.co
*/
function handleError($code, $description, $file = null, $line = null, $context = null) {
list($error, $log) = mapErrorCode($code);
$data = array(
'error' => $error,
'file' => $file,
'line' => $line,
'context' => $context,
'debug' => var_dump(debug_backtrace()),
'filter' => current_filter(),
'message' => $error . ' (' . $code . '): ' . $description
);
if(isset($_SERVER['HTTP_HOST'])) {
$data['url'] = "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
} else {
$data['cli'] = true;
}
return error_log(print_r($data,true));
}
/**
* Map an error code into an Error word, and log location.
*
* @param int $code Error code to map
* @return array Array of error word, and log location.
*/
function mapErrorCode($code) {
$error = $log = null;
switch ($code) {
case E_PARSE:
case E_ERROR:
case E_CORE_ERROR:
case E_COMPILE_ERROR:
case E_USER_ERROR:
$error = 'Fatal Error';
$log = LOG_ERR;
break;
case E_WARNING:
case E_USER_WARNING:
case E_COMPILE_WARNING:
case E_RECOVERABLE_ERROR:
$error = 'Warning';
$log = LOG_WARNING;
break;
case E_NOTICE:
case E_USER_NOTICE:
$error = 'Notice';
$log = LOG_NOTICE;
break;
case E_STRICT:
$error = 'Strict';
$log = LOG_NOTICE;
break;
case E_DEPRECATED:
case E_USER_DEPRECATED:
$error = 'Deprecated';
$log = LOG_NOTICE;
break;
default :
break;
}
return array($error, $log);
}
//calling custom error handler
set_error_handler("handleError");
function handleErrorAjax() {
if ( defined( 'DOING_AJAX' ) ) {
error_log(print_r(array($_REQUEST),true));
}
}
if (function_exists('add_action')) {
add_action('qm/collect/new_php_error', 'handleError', 99999, 5 );
// add_action('wp_loaded', 'handleErrorAjax', 99999, 5 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.