Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions config/debugbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),

/*
|--------------------------------------------------------------------------
Expand Down
6 changes: 5 additions & 1 deletion src/LaravelDebugbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
69 changes: 69 additions & 0 deletions tests/ErrorHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Barryvdh\Debugbar\Tests;

use Barryvdh\Debugbar\LaravelDebugbar;
use ReflectionObject;

class ErrorHandlerTest extends TestCase
{
/**
* Define environment setup.
*
* @param \Illuminate\Foundation\Application $app
*
* @return void
*/
protected function getEnvironmentSetUp($app)
{
parent::getEnvironmentSetUp($app);

// Force the Debugbar to Enable on test/cli applications
$app->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');
}
}
}