From 5757bf4cd4237a4c1f43de022bb16104c1487502 Mon Sep 17 00:00:00 2001 From: Elliot Anderson Date: Wed, 10 Dec 2025 14:58:42 -0500 Subject: [PATCH] Add error_level config option to filter error handler reporting (#1373) Implements feature request #1373 to allow customizing which error types are handled by the debugbar error handler. This is especially useful for PHP 8.2 projects using dynamic properties where deprecation warnings can be filtered out. - Add `error_level` config option (defaults to E_ALL) - Pass error_level to set_error_handler() as second parameter - Add tests to verify custom error level filtering works correctly Users can now exclude deprecation warnings by setting: `error_level: E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED` --- config/debugbar.php | 10 ++++++ src/LaravelDebugbar.php | 6 +++- tests/ErrorHandlerTest.php | 69 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 tests/ErrorHandlerTest.php 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