autoformat error.php

This commit is contained in:
towards-a-new-leftypol 2023-08-02 18:19:34 -04:00
parent 613fde283b
commit 823115ef9d
1 changed files with 63 additions and 63 deletions

View File

@ -1,23 +1,23 @@
<?PHP <?PHP
function error_handler($errno, $errstr, $errfile, $errline){ function error_handler($errno, $errstr, $errfile, $errline){
if(error_reporting() & $errno){ if(error_reporting() & $errno){
$config['debug']=true; $config['debug']=true;
error($errstr . ' in ' . $errfile . ' at line ' . $errline); error($errstr . ' in ' . $errfile . ' at line ' . $errline);
} }
return false; return false;
} }
function exception_handler($e){ function exception_handler($e){
error($e->getMessage()); error($e->getMessage());
} }
set_exception_handler('exception_handler'); set_exception_handler('exception_handler');
function fatal_error_handler(){ function fatal_error_handler(){
if (($error = error_get_last()) && $error['type'] == E_ERROR) { if (($error = error_get_last()) && $error['type'] == E_ERROR) {
error('Caught fatal error: ' . $error['message'] . ' in ' . $error['file'] . ' at line ' . $error['line']); error('Caught fatal error: ' . $error['message'] . ' in ' . $error['file'] . ' at line ' . $error['line']);
} }
} }
register_shutdown_function('fatal_error_handler'); register_shutdown_function('fatal_error_handler');
@ -25,37 +25,37 @@ register_shutdown_function('fatal_error_handler');
$error_recursion=false; $error_recursion=false;
function error($message, $priority = true, $debug_stuff = false) { function error($message, $priority = true, $debug_stuff = false) {
global $board, $mod, $config, $db_error, $error_recursion; global $board, $mod, $config, $db_error, $error_recursion;
if($error_recursion!==false){ if($error_recursion!==false){
die("Error recursion detected with " . $message . "<br>Original error:".$error_recursion); die("Error recursion detected with " . $message . "<br>Original error:".$error_recursion);
} }
$error_recursion=$message; $error_recursion=$message;
if (defined('STDIN')) { if (defined('STDIN')) {
// Running from CLI // Running from CLI
echo('Error: ' . $message . "\n"); echo('Error: ' . $message . "\n");
debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
die(); die();
} }
if(!empty($config)){ if(!empty($config)){
if ($config['syslog'] && $priority !== false) { if ($config['syslog'] && $priority !== false) {
// Use LOG_NOTICE instead of LOG_ERR or LOG_WARNING because most error message are not significant. // Use LOG_NOTICE instead of LOG_ERR or LOG_WARNING because most error message are not significant.
_syslog($priority !== true ? $priority : LOG_NOTICE, $message); _syslog($priority !== true ? $priority : LOG_NOTICE, $message);
} }
if ($config['debug']) { if ($config['debug']) {
$debug_stuff=array(); $debug_stuff=array();
if (isset($db_error)){ if (isset($db_error)){
$debug_stuff = array_combine(array('SQLSTATE', 'Error code', 'Error message'), $db_error); $debug_stuff = array_combine(array('SQLSTATE', 'Error code', 'Error message'), $db_error);
} }
$debug_stuff['backtrace'] = debug_backtrace(); $debug_stuff['backtrace'] = debug_backtrace();
$pw = $config['db']['password']; $pw = $config['db']['password'];
$debug_callback = function ($item) use (&$debug_callback, $pw) { $debug_callback = function ($item) use (&$debug_callback, $pw) {
if (is_array($item)) { if (is_array($item)) {
@ -64,32 +64,32 @@ function error($message, $priority = true, $debug_stuff = false) {
return ($item == $pw ? 'hunter2' : $item); return ($item == $pw ? 'hunter2' : $item);
}; };
$debug_stuff = array_map($debug_callback, $debug_stuff); $debug_stuff = array_map($debug_callback, $debug_stuff);
} }
} }
if (isset($_POST['json_response'])) { if (isset($_POST['json_response'])) {
header('Content-Type: text/json; charset=utf-8'); header('Content-Type: text/json; charset=utf-8');
$data=array('error'=>$message); $data=array('error'=>$message);
if(!empty($config) && $config['debug']){ if(!empty($config) && $config['debug']){
$data['debug']=$debug_stuff; $data['debug']=$debug_stuff;
} }
print json_encode($data); print json_encode($data);
exit(); exit();
} }
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error'); header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error');
die(Element('page.html', array( die(Element('page.html', array(
'config' => $config, 'config' => $config,
'title' => _('Error'), 'title' => _('Error'),
'subtitle' => _('An error has occured.'), 'subtitle' => _('An error has occured.'),
'body' => Element('error.html', array( 'body' => Element('error.html', array(
'config' => $config, 'config' => $config,
'message' => $message, 'message' => $message,
'mod' => $mod, 'mod' => $mod,
'board' => isset($board) ? $board : false, 'board' => isset($board) ? $board : false,
'debug' => str_replace("\n", '&#10;', utf8tohtml(print_r($debug_stuff, true))) 'debug' => str_replace("\n", '&#10;', utf8tohtml(print_r($debug_stuff, true)))
)) ))
))); )));
} }