Refactors the error handling function.

This commit is contained in:
jove 2016-08-22 21:56:32 +01:00
parent 0097496060
commit 06a6ae7bea

View File

@ -86,40 +86,35 @@ function error($message, $priority = true, $debug_stuff = false) {
die(); die();
} }
if ($config['debug'] && isset($db_error)) { if ($config['debug']) {
$debug_stuff=array();
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);
} }
if ($config['debug']) {
$debug_stuff['backtrace'] = debug_backtrace(); $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);
} }
// Return the bad request header, necessary for AJAX posts header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error');
// czaks: is it really so? the ajax errors only work when this is commented out
// better yet use it when ajax is disabled
if (!isset ($_POST['json_response'])) {
header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request');
}
// Is there a reason to disable this? // Is there a reason to disable this?
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');
die(json_encode(array( $data=array('error'=>$message);
'error' => $message if($config['debug']){
))); $data['debug']=$debug_stuff;
}
print json_encode($data);
exit();
} }
$pw = $config['db']['password'];
$debug_callback = function(&$item) use (&$debug_callback, $pw) {
if (is_array($item)) {
$item = array_filter($item, $debug_callback);
}
return ($item !== $pw || !$pw);
};
if ($debug_stuff)
$debug_stuff = array_filter($debug_stuff, $debug_callback);
die(Element('page.html', array( die(Element('page.html', array(
'config' => $config, 'config' => $config,
@ -130,7 +125,7 @@ function error($message, $priority = true, $debug_stuff = false) {
'message' => $message, 'message' => $message,
'mod' => $mod, 'mod' => $mod,
'board' => isset($board) ? $board : false, 'board' => isset($board) ? $board : false,
'debug' => is_array($debug_stuff) ? str_replace("\n", '
', utf8tohtml(print_r($debug_stuff, true))) : utf8tohtml($debug_stuff) 'debug' => str_replace("\n", '
', utf8tohtml(print_r($debug_stuff, true)))
)) ))
))); )));
} }