Skip to content

Commit 0b23fc2

Browse files
Add 'dontReportUsing' to filter exceptions to be reported (#56361)
* Add 'dontReportUsing' to filter exceptions to be reported * Fix Handler->dontReportUsing() method * Support for multiple calls and multiple callbacks for dontReportUsing() * Add test * Make test if statement nicer * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 7c90894 commit 0b23fc2

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

src/Illuminate/Foundation/Configuration/Exceptions.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,19 @@ public function dontReport(array|string $class)
150150
return $this;
151151
}
152152

153+
/**
154+
* Register a callback to determine if an exception should not be reported.
155+
*
156+
* @param callable $using
157+
* @return $this
158+
*/
159+
public function dontReportWhen(Closure $dontReportWhen)
160+
{
161+
$this->handler->dontReportWhen($dontReportWhen);
162+
163+
return $this;
164+
}
165+
153166
/**
154167
* Do not report duplicate exceptions.
155168
*

src/Illuminate/Foundation/Exceptions/Handler.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ class Handler implements ExceptionHandlerContract
7272
*/
7373
protected $dontReport = [];
7474

75+
76+
/**
77+
* The callbacks that inspect exceptions to determine if they should be reported.
78+
*
79+
* @var array
80+
*/
81+
protected $dontReportCallbacks = [];
82+
7583
/**
7684
* The callbacks that should be used during reporting.
7785
*
@@ -279,6 +287,23 @@ public function dontReport(array|string $exceptions)
279287
return $this->ignore($exceptions);
280288
}
281289

290+
/**
291+
* Register a callback to determine if an exception should not be reported.
292+
*
293+
* @param callable $dontReportWhen
294+
* @return $this
295+
*/
296+
public function dontReportWhen(callable $dontReportWhen)
297+
{
298+
if (! $dontReportWhen instanceof Closure) {
299+
$dontReportWhen = Closure::fromCallable($dontReportWhen);
300+
}
301+
302+
$this->dontReportCallbacks[] = $dontReportWhen;
303+
304+
return $this;
305+
}
306+
282307
/**
283308
* Indicate that the given exception type should not be reported.
284309
*
@@ -413,6 +438,12 @@ protected function shouldntReport(Throwable $e)
413438
return true;
414439
}
415440

441+
foreach ($this->dontReportCallbacks as $dontReportCallback) {
442+
if ($dontReportCallback($e) === true) {
443+
return true;
444+
}
445+
}
446+
416447
return rescue(fn () => with($this->throttle($e), function ($throttle) use ($e) {
417448
if ($throttle instanceof Unlimited || $throttle === null) {
418449
return false;

tests/Foundation/FoundationExceptionsHandlerTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,29 @@ public function testItCanDedupeExceptions()
635635
$this->assertSame($reported, [$one, $two]);
636636
}
637637

638+
public function testItCanSkipExceptionReportingUsingCallback()
639+
{
640+
$reported = [];
641+
$e1 = new RuntimeException('foo');
642+
$e2 = new RuntimeException('bar');
643+
644+
$this->handler->reportable(function (\Throwable $e) use (&$reported) {
645+
$reported[] = $e;
646+
647+
return false;
648+
});
649+
650+
$this->handler->dontReportWhen(function (\Throwable $e) {
651+
return $e->getMessage() === 'foo';
652+
});
653+
654+
$this->handler->report($e1);
655+
$this->handler->report($e2);
656+
$this->handler->report($e1);
657+
658+
$this->assertSame($reported, [$e2]);
659+
}
660+
638661
public function testItDoesNotThrottleExceptionsByDefault()
639662
{
640663
$reported = [];

0 commit comments

Comments
 (0)