Added helper constant for ignoring errors, ignoring errors now uses bitwise operators
This commit is contained in:
parent
6d23922b81
commit
08a03a288a
5 changed files with 53 additions and 41 deletions
|
@ -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_.
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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 ];
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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 ];
|
||||
}
|
||||
}
|
||||
}
|
38
tests/lib/HandlerInvocationTests.php
Normal file
38
tests/lib/HandlerInvocationTests.php
Normal file
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
/**
|
||||
* @license MIT
|
||||
* Copyright 2022 Dustin Wilson, et al.
|
||||
* See LICENSE and AUTHORS files for details
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
namespace MensBeam\Catcher\Test;
|
||||
use MensBeam\Catcher\{
|
||||
Error,
|
||||
Handler
|
||||
};
|
||||
|
||||
|
||||
trait HandlerInvocationTests {
|
||||
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 Error('Ook!', \E_ERROR, __FILE__, __LINE__), false, true, 'error', [ \E_ALL ] ],
|
||||
[ new Error('Ook!', \E_NOTICE, __FILE__, __LINE__), false, true, 'notice', null ],
|
||||
[ new Error('Ook!', \E_NOTICE, __FILE__, __LINE__), false, true, 'notice', [ \E_NOTICE ] ],
|
||||
[ new Error('Ook!', \E_NOTICE, __FILE__, __LINE__), false, true, 'notice', [ Handler::NON_FATAL_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 ];
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue