Skip to content

Commit

Permalink
Suppress notification if error_reporting() has narrowed
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
fuhry authored and thompiler committed Apr 18, 2024
1 parent c5c3549 commit d91a79f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 13 additions & 0 deletions tests/ErrorHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ public static function undefinedVarErrorProvider()
];
}

public static function testSuppression()
{
list($notifier, $handler) = self::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();

self::testPostsError($notifier);
}

private static function makeHandlerBoundNotifier()
{
$notifier = new NotifierMock([
Expand Down
11 changes: 11 additions & 0 deletions tests/Troublemaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

0 comments on commit d91a79f

Please sign in to comment.