From 08a03a288ab6b61f18e722ec2186465a014001ad Mon Sep 17 00:00:00 2001 From: Dustin Wilson Date: Thu, 9 Nov 2023 09:10:44 -0600 Subject: [PATCH] Added helper constant for ignoring errors, ignoring errors now uses bitwise operators --- README.md | 4 ++- lib/Catcher/Handler.php | 6 +++-- tests/cases/TestJSONHandler.php | 23 +++-------------- tests/cases/TestPlainTextHandler.php | 23 +++-------------- tests/lib/HandlerInvocationTests.php | 38 ++++++++++++++++++++++++++++ 5 files changed, 53 insertions(+), 41 deletions(-) create mode 100644 tests/lib/HandlerInvocationTests.php diff --git a/README.md b/README.md index 040b84a..4196273 100644 --- a/README.md +++ b/README.md @@ -173,6 +173,8 @@ namespace MensBeam\Catcher; abstract class Handler { public const CONTENT_TYPE = null; + public const NON_FATAL_ERROR = \E_NOTICE | \E_USER_NOTICE | \E_STRICT | \E_WARNING | \E_COMPILE_WARNING | \E_USER_WARNING | \E_DEPRECATED | \E_USER_DEPRECATED; + // Control constants public const BUBBLES = 1; public const EXIT = 2; @@ -242,7 +244,7 @@ _forceBreak_: When set this will force the stack loop to break after the handler _forceExit_: When set this will force an exit after all handlers have been run. Defaults to _false_. _forceOutputNow_: When set this will force output of the handler immediately. Defaults to _false_. _httpCode_: The HTTP code to be sent; possible values are 200, 400-599. Defaults to _500_. -_ignore_: An array of class strings or error codes to ignore. Defaults to _null_ (no ignoring). +_ignore_: An array of class strings or error code bitmasks to ignore. Defaults to _null_ (no ignoring). _logger_: The PSR-3 compatible logger in which to log to. Defaults to _null_ (no logging). _logWhenSilent_: When set to true the handler will still send logs when silent. Defaults to _true_. _outputBacktrace_: When true will output a stack trace. Defaults to _false_. diff --git a/lib/Catcher/Handler.php b/lib/Catcher/Handler.php index 3f7c871..641e0e8 100644 --- a/lib/Catcher/Handler.php +++ b/lib/Catcher/Handler.php @@ -14,6 +14,8 @@ use MensBeam\Catcher, abstract class Handler { public const CONTENT_TYPE = null; + public const NON_FATAL_ERROR = \E_NOTICE | \E_USER_NOTICE | \E_STRICT | \E_WARNING | \E_COMPILE_WARNING | \E_USER_WARNING | \E_DEPRECATED | \E_USER_DEPRECATED; + // Control constants public const BUBBLES = 1; public const EXIT = 2; @@ -51,7 +53,7 @@ abstract class Handler { /** The HTTP code to be sent; possible values: 200, 400-599 */ protected int $_httpCode = 500; /** - * An array of class strings or error codes to ignore + * An array of class strings or error code bitmasks to ignore * @var int[]|string[] */ protected array $_ignore = []; @@ -126,7 +128,7 @@ abstract class Handler { if (count($this->_ignore) > 0) { $throwable = $controller->getThrowable(); foreach ($this->_ignore as $i) { - if (($throwable instanceof Error && is_int($i) && $throwable->getCode() === $i) || (is_string($i) && $throwable instanceof $i)) { + if (($throwable instanceof Error && is_int($i) && $i & $throwable->getCode()) || (is_string($i) && $throwable instanceof $i)) { $ignore = true; break; } diff --git a/tests/cases/TestJSONHandler.php b/tests/cases/TestJSONHandler.php index 83aecd0..0c31637 100644 --- a/tests/cases/TestJSONHandler.php +++ b/tests/cases/TestJSONHandler.php @@ -22,6 +22,8 @@ use Psr\Log\LoggerInterface, * @covers \MensBeam\Catcher\Handler */ class TestJSONHandler extends \PHPUnit\Framework\TestCase { + use HandlerInvocationTests; + protected ?Handler $handler = null; @@ -60,8 +62,9 @@ class TestJSONHandler extends \PHPUnit\Framework\TestCase { if (!$silent && $ignore === null) { $u = json_decode($u, true); $this->assertEquals($c, $u['class']); - $this->assertEquals(__FILE__, $u['file']); + $this->assertEquals((new \ReflectionClass(HandlerInvocationTests::class))->getFileName(), $u['file']); $this->assertEquals($line, $u['line']); + $this->assertNotNull($h->getLastOutputThrowable()); } else { if ($ignore !== null) { $this->assertNull($h->getLastOutputThrowable()); @@ -89,22 +92,4 @@ class TestJSONHandler extends \PHPUnit\Framework\TestCase { yield $o; } } - - public static function provideInvocationTests(): iterable { - $options = [ - [ new \Exception('Ook!'), false, true, 'critical', null ], - [ new \Exception('Ook!'), false, true, 'critical', [ \Exception::class ] ], - [ new \Error('Ook!'), true, false, null, null ], - [ new \Error('Ook!'), true, false, null, [ \Error::class ] ], - [ new Error('Ook!', \E_ERROR, __FILE__, __LINE__), false, true, 'error', null ], - [ new Error('Ook!', \E_ERROR, __FILE__, __LINE__), false, true, 'error', [ \E_ERROR ] ], - [ new \Exception(message: 'Ook!', previous: new \Error(message: 'Eek!', previous: new \ParseError('Ack!'))), true, true, 'critical', null ], - [ new \Exception(message: 'Ook!', previous: new \Error(message: 'Eek!', previous: new \ParseError('Ack!'))), true, true, 'critical', [ \Exception::class ] ] - ]; - - $l = count($options); - foreach ($options as $k => $o) { - yield [ ...$o, __LINE__ - 4 - $l + $k ]; - } - } } \ No newline at end of file diff --git a/tests/cases/TestPlainTextHandler.php b/tests/cases/TestPlainTextHandler.php index c86ac8c..a90ebee 100644 --- a/tests/cases/TestPlainTextHandler.php +++ b/tests/cases/TestPlainTextHandler.php @@ -22,6 +22,8 @@ use Psr\Log\LoggerInterface, * @covers \MensBeam\Catcher\Handler */ class TestPlainTextHandler extends \PHPUnit\Framework\TestCase { + use HandlerInvocationTests; + protected ?Handler $handler = null; @@ -63,7 +65,8 @@ class TestPlainTextHandler extends \PHPUnit\Framework\TestCase { $u = substr($u, 0, strpos($u, \PHP_EOL) ?: 0); if (!$silent && $ignore === null) { - $this->assertMatchesRegularExpression(sprintf('/^\[[\d:]+\] %s: Ook\! in file %s on line %s$/', preg_quote($c, '/'), preg_quote(__FILE__, '/'), $line), $u); + $this->assertMatchesRegularExpression(sprintf('/^\[[\d:]+\] %s: Ook\! in file %s on line %s$/', preg_quote($c, '/'), preg_quote((new \ReflectionClass(HandlerInvocationTests::class))->getFileName(), '/'), $line), $u); + $this->assertNotNull($h->getLastOutputThrowable()); } else { if ($ignore !== null) { $this->assertNull($h->getLastOutputThrowable()); @@ -91,22 +94,4 @@ class TestPlainTextHandler extends \PHPUnit\Framework\TestCase { yield $o; } } - - public static function provideInvocationTests(): iterable { - $options = [ - [ new \Exception('Ook!'), false, true, 'critical', null ], - [ new \Exception('Ook!'), false, true, 'critical', [ \Exception::class ] ], - [ new \Error('Ook!'), true, false, null, null ], - [ new \Error('Ook!'), true, false, null, [ \Error::class ] ], - [ new Error('Ook!', \E_ERROR, __FILE__, __LINE__), false, true, 'error', null ], - [ new Error('Ook!', \E_ERROR, __FILE__, __LINE__), false, true, 'error', [ \E_ERROR ] ], - [ new \Exception(message: 'Ook!', previous: new \Error(message: 'Eek!', previous: new \ParseError('Ack!'))), true, true, 'critical', null ], - [ new \Exception(message: 'Ook!', previous: new \Error(message: 'Eek!', previous: new \ParseError('Ack!'))), true, true, 'critical', [ \Exception::class ] ] - ]; - - $l = count($options); - foreach ($options as $k => $o) { - yield [ ...$o, __LINE__ - 4 - $l + $k ]; - } - } } \ No newline at end of file diff --git a/tests/lib/HandlerInvocationTests.php b/tests/lib/HandlerInvocationTests.php new file mode 100644 index 0000000..9791d90 --- /dev/null +++ b/tests/lib/HandlerInvocationTests.php @@ -0,0 +1,38 @@ + $o) { + yield [ ...$o, __LINE__ - 4 - $l + $k ]; + } + } +} \ No newline at end of file