From 79764ced60a100c2860798165074c4966a3f4c77 Mon Sep 17 00:00:00 2001 From: Sarjuuk Date: Mon, 24 Jun 2024 16:50:10 +0200 Subject: [PATCH] Core/ErrorHandler * do not handle errors outside of the registered handlers * always handle all errors otherwise they get stored for error_get_last * always print errors to CLI * shutdown function handler should not be picky about what errors it gets to report * removed some mostly unused error strings --- includes/defines.php | 9 --- includes/kernel.php | 86 ++++++++++---------------- includes/utilities.php | 14 +++++ setup/tools/CLISetup.class.php | 9 +-- setup/tools/filegen/talenticons.ss.php | 2 +- 5 files changed, 52 insertions(+), 68 deletions(-) diff --git a/includes/defines.php b/includes/defines.php index 93d559e6..f9d55770 100644 --- a/includes/defines.php +++ b/includes/defines.php @@ -7,7 +7,6 @@ if (!defined('AOWOW_REVISION')) * Page */ -define('E_AOWOW', E_ALL & ~(E_DEPRECATED | E_USER_DEPRECATED | E_STRICT)); define('JSON_AOWOW_POWER', JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); define('FILTER_FLAG_STRIP_AOWOW', FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH | FILTER_FLAG_STRIP_BACKTICK); @@ -26,14 +25,6 @@ define('MIME_TYPE_RSS', 'Content-Type: application/rss+xml; charset=utf-8 define('MIME_TYPE_JPEG', 'Content-Type: image/jpeg'); define('MIME_TYPE_PNG', 'Content-Type: image/png'); -// shared setup strings -define('ERR_CREATE_FILE', 'could not create file at destination %s'); -define('ERR_WRITE_FILE', 'could not write to file at destination %s'); -define('ERR_READ_FILE', 'file %s could not be read'); -define('ERR_MISSING_FILE', 'file %s not found'); -define('ERR_NONE', 'created file %s'); -define('ERR_MISSING_INCL', 'required function %s() could not be found at %s'); - define('CACHE_TYPE_NONE', 0); // page will not be cached define('CACHE_TYPE_PAGE', 1); define('CACHE_TYPE_TOOLTIP', 2); diff --git a/includes/kernel.php b/includes/kernel.php index 23734aad..f43336c5 100644 --- a/includes/kernel.php +++ b/includes/kernel.php @@ -91,44 +91,26 @@ set_error_handler(function($errNo, $errStr, $errFile, $errLine) return true; $errName = 'unknown error'; // errors not in this list can not be handled by set_error_handler (as per documentation) or are ignored - $uGroup = U_GROUP_EMPLOYEE; - $logLevel = CLI::LOG_BLANK; + $logLevel = CLI::logLevelFromE($errNo); - if ($errNo == E_WARNING) // 0x0002 + switch ($errNo) { - $errName = 'E_WARNING'; - $logLevel = CLI::LOG_WARN; - } - else if ($errNo == E_PARSE) // 0x0004 - { - $errName = 'E_PARSE'; - $logLevel = CLI::LOG_ERROR; - } - else if ($errNo == E_NOTICE) // 0x0008 - { - $errName = 'E_NOTICE'; - $logLevel = CLI::LOG_INFO; - } - else if ($errNo == E_USER_ERROR) // 0x0100 - { - $errName = 'E_USER_ERROR'; - $logLevel = CLI::LOG_ERROR; - } - else if ($errNo == E_USER_WARNING) // 0x0200 - { - $errName = 'E_USER_WARNING'; - $logLevel = CLI::LOG_WARN; - } - else if ($errNo == E_USER_NOTICE) // 0x0400 - { - $errName = 'E_USER_NOTICE'; - $uGroup = U_GROUP_STAFF; - $logLevel = CLI::LOG_INFO; - } - else if ($errNo == E_RECOVERABLE_ERROR) // 0x1000 - { - $errName = 'E_RECOVERABLE_ERROR'; - $logLevel = CLI::LOG_ERROR; + case E_WARNING: + case E_USER_WARNING: + $errName = 'WARNING'; + break; + case E_NOTICE: + case E_USER_NOTICE: + $errName = 'NOTICE'; + break; + case E_USER_ERROR: + $errName = 'USER_ERROR'; + case E_USER_ERROR: + $errName = 'RECOVERABLE_ERROR'; + case E_STRICT: // ignore STRICT and DEPRECATED + case E_DEPRECATED: + case E_USER_DEPRECATED: + return true; } if (DB::isConnected(DB_AOWOW)) @@ -136,47 +118,43 @@ set_error_handler(function($errNo, $errStr, $errFile, $errLine) AOWOW_REVISION, $errNo, $errFile, $errLine, CLI ? 'CLI' : ($_SERVER['QUERY_STRING'] ?? ''), User::$groups, $errStr ); - if (Cfg::get('DEBUG') >= $logLevel) - { - Util::addNote($errName.' - '.$errStr.' @ '.$errFile. ':'.$errLine, $uGroup, $logLevel); - if (CLI) - CLI::write($errName.' - '.$errStr.' @ '.$errFile. ':'.$errLine, $errNo & (E_WARNING | E_USER_WARNING | E_NOTICE | E_USER_NOTICE) ? CLI::LOG_WARN : CLI::LOG_ERROR); - } + if (CLI) + CLI::write($errName.' - '.$errStr.' @ '.$errFile. ':'.$errLine, $logLevel); + else if (Cfg::get('DEBUG') >= $logLevel) + Util::addNote($errName.' - '.$errStr.' @ '.$errFile. ':'.$errLine, U_GROUP_EMPLOYEE, $logLevel); return true; -}, E_AOWOW); +}, E_ALL); // handle exceptions set_exception_handler(function ($e) { - Util::addNote('Exception - '.$e->getMessage().' @ '.$e->getFile(). ':'.$e->getLine()."\n".$e->getTraceAsString()); - if (DB::isConnected(DB_AOWOW)) DB::Aowow()->query('INSERT INTO ?_errors (`date`, `version`, `phpError`, `file`, `line`, `query`, `userGroups`, `message`) VALUES (UNIX_TIMESTAMP(), ?d, ?d, ?, ?d, ?, ?d, ?) ON DUPLICATE KEY UPDATE `date` = UNIX_TIMESTAMP()', AOWOW_REVISION, $e->getCode(), $e->getFile(), $e->getLine(), CLI ? 'CLI' : ($_SERVER['QUERY_STRING'] ?? ''), User::$groups, $e->getMessage() ); - if (!CLI) - (new GenericPage())->error(); - else + if (CLI) echo "\nException - ".$e->getMessage()."\n ".$e->getFile(). '('.$e->getLine().")\n".$e->getTraceAsString()."\n\n"; + else + { + Util::addNote('Exception - '.$e->getMessage().' @ '.$e->getFile(). ':'.$e->getLine()."\n".$e->getTraceAsString(), U_GROUP_EMPLOYEE, CLI::LOG_ERROR); + (new GenericPage())->error(); + } }); // handle fatal errors register_shutdown_function(function() { - if (($e = error_get_last()) && $e['type'] & (E_ERROR | E_COMPILE_ERROR | E_CORE_ERROR)) + if ($e = error_get_last()) { if (DB::isConnected(DB_AOWOW)) DB::Aowow()->query('INSERT INTO ?_errors (`date`, `version`, `phpError`, `file`, `line`, `query`, `userGroups`, `message`) VALUES (UNIX_TIMESTAMP(), ?d, ?d, ?, ?d, ?, ?d, ?) ON DUPLICATE KEY UPDATE `date` = UNIX_TIMESTAMP()', AOWOW_REVISION, $e['type'], $e['file'], $e['line'], CLI ? 'CLI' : ($_SERVER['QUERY_STRING'] ?? ''), User::$groups, $e['message'] ); - if (CLI) + if (CLI || User::isInGroup(U_GROUP_EMPLOYEE)) echo "\nFatal Error - ".$e['message'].' @ '.$e['file']. ':'.$e['line']."\n\n"; - - // cant generate a page for web view :( - die(); } }); @@ -208,7 +186,7 @@ Cfg::load(); // handle non-fatal errors and notices -error_reporting(Cfg::get('DEBUG') ? E_AOWOW : 0); +error_reporting(E_ALL); if (!CLI) diff --git a/includes/utilities.php b/includes/utilities.php index a7bb2d16..d4b7bf36 100644 --- a/includes/utilities.php +++ b/includes/utilities.php @@ -309,6 +309,20 @@ abstract class CLI flush(); } + public static function logLevelFromE(int $phpError) : int + { + if ($phpError & (E_ERROR | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR)) + return self::LOG_ERROR; + + if ($phpError & (E_WARNING | E_USER_WARNING | E_NOTICE | E_USER_NOTICE | E_CORE_WARNING | E_COMPILE_WARNING)) + return self::LOG_WARN; + + if ($phpError & (E_STRICT | E_NOTICE | E_USER_NOTICE | E_DEPRECATED | E_USER_DEPRECATED)) + return self::LOG_INFO; + + return self::LOG_BLANK; + } + private static function purgeEscapes(string $msg) : string { return preg_replace(["/\e\[[\d;]+[mK]/", "/\e\[\d+G/"], ['', "\n"], $msg); diff --git a/setup/tools/CLISetup.class.php b/setup/tools/CLISetup.class.php index 929ab315..88b2816f 100644 --- a/setup/tools/CLISetup.class.php +++ b/setup/tools/CLISetup.class.php @@ -635,21 +635,22 @@ class CLISetup { if (Util::writeFile($file, $content)) { - CLI::write(sprintf(ERR_NONE, CLI::bold($file)), CLI::LOG_OK, true, true); + CLI::write('created file '. CLI::bold($file), CLI::LOG_OK, true, true); return true; } - $e = error_get_last(); - CLI::write($e['message'].' '.CLI::bold($file), CLI::LOG_ERROR); return false; } public static function writeDir(string $dir, bool &$exist = true) : bool { if (Util::writeDir($dir, $exist)) + { + if (!$exist) + CLI::write('created dir '. CLI::bold($dir), CLI::LOG_OK, true, true); return true; + } - CLI::write(error_get_last()['message'].' '.CLI::bold($dir), CLI::LOG_ERROR); return false; } diff --git a/setup/tools/filegen/talenticons.ss.php b/setup/tools/filegen/talenticons.ss.php index ac41d8f5..718660d8 100644 --- a/setup/tools/filegen/talenticons.ss.php +++ b/setup/tools/filegen/talenticons.ss.php @@ -91,7 +91,7 @@ CLISetup::registerSetup("build", new class extends SetupScript } if (@imagejpeg($res, $outFile)) - CLI::write('[talenticons] '.sprintf(ERR_NONE, CLI::bold($outFile)), CLI::LOG_OK, true, true); + CLI::write('[talenticons] created file '.CLI::bold($outFile), CLI::LOG_OK, true, true); else { $this->success = false;