From 95f161414de339a57a3b18068101359869b0d00a Mon Sep 17 00:00:00 2001 From: Dan Fuhry Date: Mon, 13 Jul 2020 13:54:49 -0400 Subject: [PATCH] Suppress notification if error_reporting() has narrowed If the error_reporting() setting has narrowed since Airbrake's error handler was installed, respect the new setting. This also resolves the issue of errors suppressed with the "@" operator being reported to Airbrake. (Fixes #105) --- src/ErrorHandler.php | 7 +++++++ tests/ErrorHandlerTest.php | 15 ++++++++++++++- tests/Troublemaker.php | 11 +++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/ErrorHandler.php b/src/ErrorHandler.php index 8ca42cb..a03a1e2 100644 --- a/src/ErrorHandler.php +++ b/src/ErrorHandler.php @@ -31,6 +31,13 @@ public function __construct(\Airbrake\Notifier $notifier) */ public function onError($code, $message, $file, $line) { + // If error_reporting() setting has changed since the ErrorHandler was + // installed, respect the new settings. This also respects the + // @-operator (issue #105) + if ((error_reporting() & $code) === 0) { + return false; + } + $this->lastError = [ 'message' => $message, 'file' => $file, diff --git a/tests/ErrorHandlerTest.php b/tests/ErrorHandlerTest.php index 3aa25c9..46c124e 100644 --- a/tests/ErrorHandlerTest.php +++ b/tests/ErrorHandlerTest.php @@ -33,6 +33,19 @@ public function undefinedVarErrorProvider() ]; } + public function testSuppression() + { + list($notifier, $handler) = $this->makeHandlerBoundNotifier(); + + // Cause two errors in a row, but suppress the second one. Verify that + // the notifier's most recent notice corresponds to the first error + // generated. + Troublemaker::echoUndefinedVar(); + @Troublemaker::echoUndefinedIndex(); + + $this->testPostsError($notifier); + } + private function makeHandlerBoundNotifier() { $notifier = new NotifierMock([ @@ -58,7 +71,7 @@ private function arrangeOnErrorNotifier() private function arrangeOnShutdownNotifier() { list($notifier, $handler) = $this->makeHandlerBoundNotifier(); - @Troublemaker::echoUndefinedVar(); + Troublemaker::echoUndefinedVar(); $handler->onShutdown(); return $notifier; diff --git a/tests/Troublemaker.php b/tests/Troublemaker.php index a937fa3..0a1eca8 100644 --- a/tests/Troublemaker.php +++ b/tests/Troublemaker.php @@ -42,4 +42,15 @@ public static function newNestedException() return new \Exception('world', 207, $e); } } + + private static function doEchoUndefinedIndex() + { + $foo = []; + echo $foo[0]; + } + + public static function echoUndefinedIndex() + { + self::doEchoUndefinedIndex(); + } }