Fixed Logger::exceptionHandler() expecting Exception classes while Throwable classes could also be expected. (#12)
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is passing Details

- Amended Logger
- Added unit tests for bug
- Added PHP8.1 to CI testing

Reviewed-on: #12
This commit is contained in:
Abel Hoogeveen 2022-02-20 12:11:58 +00:00
parent ed0a82d68b
commit ccb0564a14
3 changed files with 34 additions and 6 deletions

View File

@ -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:

View File

@ -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

View File

@ -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
*/