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: #12
This commit is contained in:
parent
ed0a82d68b
commit
ccb0564a14
|
@ -20,6 +20,12 @@ steps:
|
||||||
- docker-php-ext-enable xdebug
|
- docker-php-ext-enable xdebug
|
||||||
- vendor/bin/phpunit -c test/phpunit.xml
|
- 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
|
- name: coverage
|
||||||
image: registry.i15.nl/i15/fuzephp:8.0-alpine
|
image: registry.i15.nl/i15/fuzephp:8.0-alpine
|
||||||
commands:
|
commands:
|
||||||
|
|
|
@ -36,9 +36,10 @@
|
||||||
|
|
||||||
namespace FuzeWorks;
|
namespace FuzeWorks;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
use FuzeWorks\Exception\ConfigException;
|
use FuzeWorks\Exception\ConfigException;
|
||||||
use FuzeWorks\Exception\EventException;
|
use FuzeWorks\Exception\EventException;
|
||||||
use Exception;
|
use Throwable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Logger Class.
|
* Logger Class.
|
||||||
|
@ -279,20 +280,21 @@ class Logger {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Exception handler
|
* 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.
|
* 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
|
* @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(),
|
'message' => $exception->getMessage(),
|
||||||
'logFile' => $exception->getFile(),
|
'logFile' => $exception->getFile(),
|
||||||
'logLine' => $exception->getLine(),
|
'logLine' => $exception->getLine(),
|
||||||
'context' => $exception->getTraceAsString(),
|
'context' => $exception->getTraceAsString(),
|
||||||
'runtime' => round(self::getRelativeTime(), 4),);
|
'runtime' => round(self::getRelativeTime(), 4),];
|
||||||
self::$logs[] = $LOG;
|
self::$logs[] = $LOG;
|
||||||
|
|
||||||
// And return a 500 because this error was fatal
|
// And return a 500 because this error was fatal
|
||||||
|
|
|
@ -135,12 +135,32 @@ class loggerTest extends CoreTestAbstract
|
||||||
Events::addListener(function($event){
|
Events::addListener(function($event){
|
||||||
$event->setCancelled(true);
|
$event->setCancelled(true);
|
||||||
$this->assertEquals('FAILURE', $event->log['message']);
|
$this->assertEquals('FAILURE', $event->log['message']);
|
||||||
|
$this->assertEquals("EXCEPTION", $event->log['type']);
|
||||||
}, 'haltExecutionEvent');
|
}, 'haltExecutionEvent');
|
||||||
|
|
||||||
// Log the exception
|
// Log the exception
|
||||||
Logger::exceptionHandler($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
|
* @covers ::log
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue