From 77b99b2c4f64b73b062f2d542fbf04903a1d814d Mon Sep 17 00:00:00 2001 From: jove Date: Mon, 22 Aug 2016 23:39:36 +0100 Subject: Finishes error reworking. --- inc/error.php | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 inc/error.php (limited to 'inc/error.php') diff --git a/inc/error.php b/inc/error.php new file mode 100644 index 00000000..9452e393 --- /dev/null +++ b/inc/error.php @@ -0,0 +1,83 @@ +getMessage()); +} + +set_exception_handler('exception_handler'); + +$error_recursion=false; + +function error($message, $priority = true, $debug_stuff = false) { + global $board, $mod, $config, $db_error, $error_recursion; + if($error_recursion!==false){ + die("Error recursion detected with " . $message . "
Original error:".$error_recursion); + } + $error_recursion=$message; + + if(!empty($config)){ + + if ($config['syslog'] && $priority !== false) { + // Use LOG_NOTICE instead of LOG_ERR or LOG_WARNING because most error message are not significant. + _syslog($priority !== true ? $priority : LOG_NOTICE, $message); + } + + if (defined('STDIN')) { + // Running from CLI + echo('Error: ' . $message . "\n"); + debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); + die(); + } + + if ($config['debug']) { + $debug_stuff=array(); + if(isset($db_error)){ + $debug_stuff = array_combine(array('SQLSTATE', 'Error code', 'Error message'), $db_error); + } + $debug_stuff['backtrace'] = debug_backtrace(); + $pw = $config['db']['password']; + $debug_callback = function(&$item) use (&$debug_callback, $pw) { + if (is_array($item)) { + return array_map($item, $debug_callback); + } + return ($item == $pw ? 'hunter2' : $item); + }; + $debug_stuff = array_map($debug_stuff, $debug_callback); + } + } + + // Is there a reason to disable this? + if (isset($_POST['json_response'])) { + header('Content-Type: text/json; charset=utf-8'); + $data=array('error'=>$message); + if(!empty($config) && $config['debug']){ + $data['debug']=$debug_stuff; + } + print json_encode($data); + exit(); + } + + header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error'); + + die(Element('page.html', array( + 'config' => $config, + 'title' => _('Error'), + 'subtitle' => _('An error has occured.'), + 'body' => Element('error.html', array( + 'config' => $config, + 'message' => $message, + 'mod' => $mod, + 'board' => isset($board) ? $board : false, + 'debug' => str_replace("\n", ' ', utf8tohtml(print_r($debug_stuff, true))) + )) + ))); +} -- cgit v1.3 From f0a625b238ce09a2e5f905ca64f480f38d8ca668 Mon Sep 17 00:00:00 2001 From: jove Date: Wed, 31 Aug 2016 20:51:09 +0100 Subject: Adds proper fatal error handling and fixes CLI error reporting. --- :q | 9 --------- inc/error.php | 25 +++++++++++++++++-------- post.php | 2 +- 3 files changed, 18 insertions(+), 18 deletions(-) delete mode 100644 :q (limited to 'inc/error.php') diff --git a/:q b/:q deleted file mode 100644 index 1423a79e..00000000 --- a/:q +++ /dev/null @@ -1,9 +0,0 @@ - -# Please enter the commit message for your changes. Lines starting -# with '#' will be ignored, and an empty message aborts the commit. -# On branch installfix -# Changes to be committed: -# modified: install.php -This prevents a crash due to instance-config.php not existing, it will now check if the file exists and if not creates the file - - diff --git a/inc/error.php b/inc/error.php index 9452e393..5138592c 100644 --- a/inc/error.php +++ b/inc/error.php @@ -15,15 +15,32 @@ function exception_handler(Exception $e){ set_exception_handler('exception_handler'); +function fatal_error_handler(){ + if (($error = error_get_last()) && $error['type'] == E_ERROR) { + error('Caught fatal error: ' . $error['message'] . ' in ' . $error['file'] . ' at line ' . $error['line']); + } +} + +register_shutdown_function('fatal_error_handler'); + $error_recursion=false; function error($message, $priority = true, $debug_stuff = false) { global $board, $mod, $config, $db_error, $error_recursion; + if($error_recursion!==false){ die("Error recursion detected with " . $message . "
Original error:".$error_recursion); } + $error_recursion=$message; + if (defined('STDIN')) { + // Running from CLI + echo('Error: ' . $message . "\n"); + debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); + die(); + } + if(!empty($config)){ if ($config['syslog'] && $priority !== false) { @@ -31,13 +48,6 @@ function error($message, $priority = true, $debug_stuff = false) { _syslog($priority !== true ? $priority : LOG_NOTICE, $message); } - if (defined('STDIN')) { - // Running from CLI - echo('Error: ' . $message . "\n"); - debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); - die(); - } - if ($config['debug']) { $debug_stuff=array(); if(isset($db_error)){ @@ -55,7 +65,6 @@ function error($message, $priority = true, $debug_stuff = false) { } } - // Is there a reason to disable this? if (isset($_POST['json_response'])) { header('Content-Type: text/json; charset=utf-8'); $data=array('error'=>$message); diff --git a/post.php b/post.php index 0022f178..99ef4850 100644 --- a/post.php +++ b/post.php @@ -325,7 +325,7 @@ if (isset($_POST['delete'])) { $post['file_tmp'] = tempnam($config['tmp'], 'url'); function unlink_tmp_file($file) { @unlink($file); - error(); + fatal_error_handler(); } register_shutdown_function('unlink_tmp_file', $post['file_tmp']); -- cgit v1.3