From df2ca95675204d44aa9170aef1bd6b8f8b3ad70d Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Tue, 22 Jul 2025 01:59:11 +0200 Subject: [PATCH 1/6] Add 'dontReportUsing' to filter exceptions to be reported --- .../Foundation/Configuration/Exceptions.php | 13 +++++++++++ .../Foundation/Exceptions/Handler.php | 23 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/Illuminate/Foundation/Configuration/Exceptions.php b/src/Illuminate/Foundation/Configuration/Exceptions.php index 1072a1431196..233e18ab7825 100644 --- a/src/Illuminate/Foundation/Configuration/Exceptions.php +++ b/src/Illuminate/Foundation/Configuration/Exceptions.php @@ -150,6 +150,19 @@ public function dontReport(array|string $class) return $this; } + /** + * Register a callback to determine if an exception should not be reported. + * + * @param callable $using + * @return Exceptions + */ + public function dontReportUsing(Closure $dontReportUsing) + { + $this->handler->dontReportUsing($dontReportUsing); + + return $this; + } + /** * Do not report duplicate exceptions. * diff --git a/src/Illuminate/Foundation/Exceptions/Handler.php b/src/Illuminate/Foundation/Exceptions/Handler.php index 00a69266dcc7..c225dff15b04 100644 --- a/src/Illuminate/Foundation/Exceptions/Handler.php +++ b/src/Illuminate/Foundation/Exceptions/Handler.php @@ -72,6 +72,14 @@ class Handler implements ExceptionHandlerContract */ protected $dontReport = []; + + /** + * A callback that inspects exceptions to determine if they should be reported. + * + * @var callable|null + */ + protected $dontReportCallback = null; + /** * The callbacks that should be used during reporting. * @@ -279,6 +287,17 @@ public function dontReport(array|string $exceptions) return $this->ignore($exceptions); } + /** + * Register a callback to determine if an exception should not be reported. + * + * @param array|string $class + * @return $this + */ + public function dontReportUsing(callable $dontReportUsing) + { + $this->dontReportCallback = $dontReportUsing; + } + /** * Indicate that the given exception type should not be reported. * @@ -413,6 +432,10 @@ protected function shouldntReport(Throwable $e) return true; } + if ($this->dontReportCallback && $this->container->call($this->dontReportCallback, [$e])) { + return true; + } + return rescue(fn () => with($this->throttle($e), function ($throttle) use ($e) { if ($throttle instanceof Unlimited || $throttle === null) { return false; From f2cbdc564bb72ee78e3b60abb415829d6504e4c7 Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Tue, 22 Jul 2025 02:08:55 +0200 Subject: [PATCH 2/6] Fix Handler->dontReportUsing() method --- src/Illuminate/Foundation/Exceptions/Handler.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Foundation/Exceptions/Handler.php b/src/Illuminate/Foundation/Exceptions/Handler.php index c225dff15b04..3b5c2fec7bdc 100644 --- a/src/Illuminate/Foundation/Exceptions/Handler.php +++ b/src/Illuminate/Foundation/Exceptions/Handler.php @@ -290,12 +290,18 @@ public function dontReport(array|string $exceptions) /** * Register a callback to determine if an exception should not be reported. * - * @param array|string $class + * @param callable $dontReportUsing * @return $this */ public function dontReportUsing(callable $dontReportUsing) { + if (! $dontReportUsing instanceof Closure) { + $dontReportUsing = Closure::fromCallable($dontReportUsing); + } + $this->dontReportCallback = $dontReportUsing; + + return $this; } /** From 66d269ad6024dce6dd8e06491cb41381a29bf8aa Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Tue, 22 Jul 2025 02:37:20 +0200 Subject: [PATCH 3/6] Support for multiple calls and multiple callbacks for dontReportUsing() --- src/Illuminate/Foundation/Exceptions/Handler.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Foundation/Exceptions/Handler.php b/src/Illuminate/Foundation/Exceptions/Handler.php index 3b5c2fec7bdc..f9395d3aec8b 100644 --- a/src/Illuminate/Foundation/Exceptions/Handler.php +++ b/src/Illuminate/Foundation/Exceptions/Handler.php @@ -78,7 +78,7 @@ class Handler implements ExceptionHandlerContract * * @var callable|null */ - protected $dontReportCallback = null; + protected $dontReportCallback = []; /** * The callbacks that should be used during reporting. @@ -299,7 +299,7 @@ public function dontReportUsing(callable $dontReportUsing) $dontReportUsing = Closure::fromCallable($dontReportUsing); } - $this->dontReportCallback = $dontReportUsing; + $this->dontReportCallback[] = $dontReportUsing; return $this; } @@ -438,8 +438,10 @@ protected function shouldntReport(Throwable $e) return true; } - if ($this->dontReportCallback && $this->container->call($this->dontReportCallback, [$e])) { - return true; + foreach ($this->dontReportCallback as $dontReportCallback) { + if ($dontReportCallback($e) === true) { + return true; + } } return rescue(fn () => with($this->throttle($e), function ($throttle) use ($e) { From 1a189ad04311af8862e9136fa54b251ba7ef97cb Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Tue, 22 Jul 2025 02:37:30 +0200 Subject: [PATCH 4/6] Add test --- .../FoundationExceptionsHandlerTest.php | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/Foundation/FoundationExceptionsHandlerTest.php b/tests/Foundation/FoundationExceptionsHandlerTest.php index 98af2559a578..4af2b815054e 100644 --- a/tests/Foundation/FoundationExceptionsHandlerTest.php +++ b/tests/Foundation/FoundationExceptionsHandlerTest.php @@ -635,6 +635,32 @@ public function testItCanDedupeExceptions() $this->assertSame($reported, [$one, $two]); } + public function testItCanSkipExceptionReportingUsingCallback() + { + $reported = []; + $e1 = new RuntimeException('foo'); + $e2 = new RuntimeException('bar'); + + $this->handler->reportable(function (\Throwable $e) use (&$reported) { + $reported[] = $e; + + return false; + }); + + $this->handler->dontReportUsing(function (\Throwable $e) { + if ($e->getMessage() === 'foo') { + return true; + } + return false; + }); + + $this->handler->report($e1); + $this->handler->report($e2); + $this->handler->report($e1); + + $this->assertSame($reported, [$e2]); + } + public function testItDoesNotThrottleExceptionsByDefault() { $reported = []; From e89ba0d4d67f7ee34c67e6d172618de092096d66 Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Tue, 22 Jul 2025 02:38:17 +0200 Subject: [PATCH 5/6] Make test if statement nicer --- tests/Foundation/FoundationExceptionsHandlerTest.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/Foundation/FoundationExceptionsHandlerTest.php b/tests/Foundation/FoundationExceptionsHandlerTest.php index 4af2b815054e..04a3fb0020f3 100644 --- a/tests/Foundation/FoundationExceptionsHandlerTest.php +++ b/tests/Foundation/FoundationExceptionsHandlerTest.php @@ -648,10 +648,7 @@ public function testItCanSkipExceptionReportingUsingCallback() }); $this->handler->dontReportUsing(function (\Throwable $e) { - if ($e->getMessage() === 'foo') { - return true; - } - return false; + return $e->getMessage() === 'foo'; }); $this->handler->report($e1); From 74762ba22ffed656b4982d420398907be7ceef1e Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 22 Jul 2025 10:30:17 -0500 Subject: [PATCH 6/6] formatting --- .../Foundation/Configuration/Exceptions.php | 8 ++++---- .../Foundation/Exceptions/Handler.php | 18 +++++++++--------- .../FoundationExceptionsHandlerTest.php | 2 +- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Illuminate/Foundation/Configuration/Exceptions.php b/src/Illuminate/Foundation/Configuration/Exceptions.php index 233e18ab7825..847c2d499b01 100644 --- a/src/Illuminate/Foundation/Configuration/Exceptions.php +++ b/src/Illuminate/Foundation/Configuration/Exceptions.php @@ -153,12 +153,12 @@ public function dontReport(array|string $class) /** * Register a callback to determine if an exception should not be reported. * - * @param callable $using - * @return Exceptions + * @param callable $using + * @return $this */ - public function dontReportUsing(Closure $dontReportUsing) + public function dontReportWhen(Closure $dontReportWhen) { - $this->handler->dontReportUsing($dontReportUsing); + $this->handler->dontReportWhen($dontReportWhen); return $this; } diff --git a/src/Illuminate/Foundation/Exceptions/Handler.php b/src/Illuminate/Foundation/Exceptions/Handler.php index f9395d3aec8b..b7b9d3e6c839 100644 --- a/src/Illuminate/Foundation/Exceptions/Handler.php +++ b/src/Illuminate/Foundation/Exceptions/Handler.php @@ -74,11 +74,11 @@ class Handler implements ExceptionHandlerContract /** - * A callback that inspects exceptions to determine if they should be reported. + * The callbacks that inspect exceptions to determine if they should be reported. * - * @var callable|null + * @var array */ - protected $dontReportCallback = []; + protected $dontReportCallbacks = []; /** * The callbacks that should be used during reporting. @@ -290,16 +290,16 @@ public function dontReport(array|string $exceptions) /** * Register a callback to determine if an exception should not be reported. * - * @param callable $dontReportUsing + * @param callable $dontReportWhen * @return $this */ - public function dontReportUsing(callable $dontReportUsing) + public function dontReportWhen(callable $dontReportWhen) { - if (! $dontReportUsing instanceof Closure) { - $dontReportUsing = Closure::fromCallable($dontReportUsing); + if (! $dontReportWhen instanceof Closure) { + $dontReportWhen = Closure::fromCallable($dontReportWhen); } - $this->dontReportCallback[] = $dontReportUsing; + $this->dontReportCallbacks[] = $dontReportWhen; return $this; } @@ -438,7 +438,7 @@ protected function shouldntReport(Throwable $e) return true; } - foreach ($this->dontReportCallback as $dontReportCallback) { + foreach ($this->dontReportCallbacks as $dontReportCallback) { if ($dontReportCallback($e) === true) { return true; } diff --git a/tests/Foundation/FoundationExceptionsHandlerTest.php b/tests/Foundation/FoundationExceptionsHandlerTest.php index 04a3fb0020f3..3797cf246037 100644 --- a/tests/Foundation/FoundationExceptionsHandlerTest.php +++ b/tests/Foundation/FoundationExceptionsHandlerTest.php @@ -647,7 +647,7 @@ public function testItCanSkipExceptionReportingUsingCallback() return false; }); - $this->handler->dontReportUsing(function (\Throwable $e) { + $this->handler->dontReportWhen(function (\Throwable $e) { return $e->getMessage() === 'foo'; });