From ccb0564a148939ee1970048c3ae9f92a6a5c1c66 Mon Sep 17 00:00:00 2001 From: Abel Hoogeveen Date: Sun, 20 Feb 2022 12:11:58 +0000 Subject: [PATCH] Fixed Logger::exceptionHandler() expecting Exception classes while Throwable classes could also be expected. (#12) - Amended Logger - Added unit tests for bug - Added PHP8.1 to CI testing Reviewed-on: https://gitea.i15.nl/FuzeWorks/Core/pulls/12 --- .drone.yml | 6 ++++++ src/FuzeWorks/Logger.php | 14 ++++++++------ test/core/core_loggerTest.php | 20 ++++++++++++++++++++ 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/.drone.yml b/.drone.yml index ec6e2a6..9354064 100644 --- a/.drone.yml +++ b/.drone.yml @@ -20,6 +20,12 @@ steps: - docker-php-ext-enable xdebug - vendor/bin/phpunit -c test/phpunit.xml + - name: php81test + image: registry.i15.nl/i15/fuzephp:8.1-alpine + commands: + - docker-php-ext-enable xdebug + - vendor/bin/phpunit -c test/phpunit.xml + - name: coverage image: registry.i15.nl/i15/fuzephp:8.0-alpine commands: diff --git a/src/FuzeWorks/Logger.php b/src/FuzeWorks/Logger.php index 476d6d8..9bb9e75 100644 --- a/src/FuzeWorks/Logger.php +++ b/src/FuzeWorks/Logger.php @@ -36,9 +36,10 @@ namespace FuzeWorks; +use Exception; use FuzeWorks\Exception\ConfigException; use FuzeWorks\Exception\EventException; -use Exception; +use Throwable; /** * Logger Class. @@ -279,20 +280,21 @@ class Logger { /** * Exception handler - * Will be triggered when an uncaught exception occures. This function shows the error-message, and shuts down the script. + * Will be triggered when an uncaught exception occurs. This function shows the error-message, and shuts down the script. * Please note that most of the user-defined exceptions will be caught in the router, and handled with the error-controller. * - * @param Exception $exception The occured exception. + * @param Throwable $exception The occurred exception. * @param bool $haltExecution. Defaults to true */ - public static function exceptionHandler(Exception $exception, bool $haltExecution = true) + public static function exceptionHandler(Throwable $exception, bool $haltExecution = true) { - $LOG = array('type' => 'EXCEPTION', + $LOG = [ + 'type' => $exception instanceof Exception ? "EXCEPTION" : "ERROR", 'message' => $exception->getMessage(), 'logFile' => $exception->getFile(), 'logLine' => $exception->getLine(), 'context' => $exception->getTraceAsString(), - 'runtime' => round(self::getRelativeTime(), 4),); + 'runtime' => round(self::getRelativeTime(), 4),]; self::$logs[] = $LOG; // And return a 500 because this error was fatal diff --git a/test/core/core_loggerTest.php b/test/core/core_loggerTest.php index c728465..cf073c0 100644 --- a/test/core/core_loggerTest.php +++ b/test/core/core_loggerTest.php @@ -135,12 +135,32 @@ class loggerTest extends CoreTestAbstract Events::addListener(function($event){ $event->setCancelled(true); $this->assertEquals('FAILURE', $event->log['message']); + $this->assertEquals("EXCEPTION", $event->log['type']); }, 'haltExecutionEvent'); // Log the exception Logger::exceptionHandler($exception); } + /** + * @covers ::exceptionHandler + * @depends testExceptionHandler + */ + public function testErrorsToExceptionHandler() + { + // Create the error + $error = new ParseError("FAILURE_ERROR"); + + // Prepare to intercept + Events::addListener(function ($event) { + $event->setCancelled(true); + $this->assertEquals("FAILURE_ERROR", $event->log['message']); + $this->assertEquals("ERROR", $event->log['type']); + }, 'haltExecutionEvent'); + + Logger::exceptionHandler($error); + } + /** * @covers ::log */