From 7c99edddbdaedd12ef5bb89c6761a510ddf56512 Mon Sep 17 00:00:00 2001 From: netkas Date: Wed, 30 Oct 2024 00:14:51 -0400 Subject: [PATCH] Implement enhanced error and exception handling --- CHANGELOG.md | 1 + src/LogLib/Log.php | 60 ++++++++++++++++++++++++++++++++++++---------- 2 files changed, 48 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4fb8dd9..b499573 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ This update introduces minor improvements ### Changed - Refactored exception handling in FileLogging where it will always attempt to print the exception no matter the log level for as long as the log level isn't silent + - Implement enhanced error and exception handling ## [2.0.1] - 2024-10-29 diff --git a/src/LogLib/Log.php b/src/LogLib/Log.php index 73b6573..da4c823 100644 --- a/src/LogLib/Log.php +++ b/src/LogLib/Log.php @@ -213,11 +213,13 @@ */ public static function registerExceptionHandler(): void { - set_exception_handler(static function(Throwable $throwable) + + // Handle uncaught exceptions + set_exception_handler(static function(Throwable $exception): void { try { - self::error('Runtime', $throwable->getMessage(), $throwable); + self::error('Runtime', $exception->getMessage(), $exception); } catch(Exception) { @@ -225,28 +227,61 @@ } }); - // Register error handler - set_error_handler(static function($errno, $errstr, $errfile, $errline) + // Handle warnings and notices + set_error_handler(static function(int $errno, string $errstr, string $errfile = '', int $errline = 0): bool { - // Convert error to exception and throw it try { - self::warning('Runtime', sprintf("%s:%s (%s) %s", $errfile, $errline, $errno, $errstr)); + // Convert error to exception for consistent handling + $exception = new ErrorException($errstr, 0, $errno, $errfile, $errline); + + // Handle different error types + switch ($errno) + { + case E_ERROR: + case E_PARSE: + case E_CORE_ERROR: + case E_COMPILE_ERROR: + case E_USER_ERROR: + self::error('Runtime', $errstr, $exception); + break; + + case E_USER_DEPRECATED: + case E_DEPRECATED: + case E_USER_NOTICE: + case E_NOTICE: + case E_USER_WARNING: + case E_WARNING: + default: + self::warning('Runtime', $errstr, $exception); + break; + } } catch(Exception) { - return; + return false; } + + // Return true to prevent PHP's internal error handler + return true; }); - register_shutdown_function(static function() + // Handle fatal errors + register_shutdown_function(static function(): void { $error = error_get_last(); - if ($error !== null && ($error['type'] & (E_ERROR | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR))) + + if ($error !== null && in_array($error['type'], [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR])) { - // Convert fatal error to exception and handle it - $exception = new ErrorException($error['message'], 0, $error['type'], $error['file'], $error['line']); - self::error('Fatal', $exception->getMessage(), $exception); + try + { + $exception = new ErrorException($error['message'], 0, $error['type'], $error['file'], $error['line']); + self::error('Fatal Error', $error['message'], $exception); + } + catch(Exception) + { + return; + } } }); } @@ -260,5 +295,4 @@ { set_exception_handler(null); } - } \ No newline at end of file