diff --git a/config/debugbar.php b/config/debugbar.php index 8ce799a8..cd32e947 100644 --- a/config/debugbar.php +++ b/config/debugbar.php @@ -139,8 +139,18 @@ | When enabled, the Debugbar shows deprecated warnings for Symfony components | in the Messages tab. | + | You can set a custom error reporting level to filter which errors are + | handled. For example, to exclude deprecation warnings: + | E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED + | + | To exclude notices, strict warnings, and deprecations: + | E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED & ~E_USER_DEPRECATED + | + | Defaults to E_ALL (all errors). + | */ 'error_handler' => env('DEBUGBAR_ERROR_HANDLER', false), + 'error_level' => env('DEBUGBAR_ERROR_LEVEL', E_ALL), /* |-------------------------------------------------------------------------- diff --git a/src/LaravelDebugbar.php b/src/LaravelDebugbar.php index 6ef7d8ea..bcdadd82 100644 --- a/src/LaravelDebugbar.php +++ b/src/LaravelDebugbar.php @@ -181,7 +181,11 @@ public function boot() // Set custom error handler if ($config->get('debugbar.error_handler', false)) { - $this->prevErrorHandler = set_error_handler([$this, 'handleError']); + // Get the error_level config, default to E_ALL + $errorLevel = $config->get('debugbar.error_level', E_ALL); + + // set error handler with configured error reporting level + $this->prevErrorHandler = set_error_handler([$this, 'handleError'], $errorLevel); } $this->selectStorage($this); diff --git a/tests/ErrorHandlerTest.php b/tests/ErrorHandlerTest.php new file mode 100644 index 00000000..3179d97a --- /dev/null +++ b/tests/ErrorHandlerTest.php @@ -0,0 +1,69 @@ +resolving(LaravelDebugbar::class, function ($debugbar) { + $refObject = new ReflectionObject($debugbar); + $refProperty = $refObject->getProperty('enabled'); + $refProperty->setValue($debugbar, true); + }); + + // Enable collectors needed for error handling + $app['config']->set('debugbar.collectors.messages', true); + $app['config']->set('debugbar.collectors.exceptions', true); + } + + public function testErrorHandlerRespectsCustomErrorLevel() + { + $app = $this->app; + $app['config']->set('debugbar.error_handler', true); + // Exclude deprecation warnings + $app['config']->set('debugbar.error_level', E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED); + + $debugbar = $app->make(LaravelDebugbar::class); + $debugbar->boot(); + + // Get initial message count + $initialCount = 0; + if ($debugbar->hasCollector('messages')) { + $initialCount = count($debugbar->getCollector('messages')->collect()['messages']); + } + + // Trigger a deprecation warning - should NOT be captured + @trigger_error('Test deprecation warning', E_USER_DEPRECATED); + + // Check that error was NOT captured + if ($debugbar->hasCollector('messages')) { + $messages = $debugbar->getCollector('messages')->collect(); + $newCount = count($messages['messages']); + $this->assertEquals($initialCount, $newCount, 'Deprecation warning should not be captured when excluded from error_level'); + } + + // Trigger a warning (not a deprecation) - should be captured + @trigger_error('Test warning', E_USER_WARNING); + + // Check that warning WAS captured + if ($debugbar->hasCollector('messages')) { + $messages = $debugbar->getCollector('messages')->collect(); + $finalCount = count($messages['messages']); + $this->assertGreaterThan($initialCount, $finalCount, 'Non-deprecation errors should still be captured'); + } + } +} \ No newline at end of file