Skip to content
Merged
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
13 changes: 13 additions & 0 deletions src/Illuminate/Foundation/Configuration/Exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 $this
*/
public function dontReportWhen(Closure $dontReportWhen)
{
$this->handler->dontReportWhen($dontReportWhen);

return $this;
}

/**
* Do not report duplicate exceptions.
*
Expand Down
31 changes: 31 additions & 0 deletions src/Illuminate/Foundation/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ class Handler implements ExceptionHandlerContract
*/
protected $dontReport = [];


/**
* The callbacks that inspect exceptions to determine if they should be reported.
*
* @var array
*/
protected $dontReportCallbacks = [];

/**
* The callbacks that should be used during reporting.
*
Expand Down Expand Up @@ -279,6 +287,23 @@ public function dontReport(array|string $exceptions)
return $this->ignore($exceptions);
}

/**
* Register a callback to determine if an exception should not be reported.
*
* @param callable $dontReportWhen
* @return $this
*/
public function dontReportWhen(callable $dontReportWhen)
{
if (! $dontReportWhen instanceof Closure) {
$dontReportWhen = Closure::fromCallable($dontReportWhen);
}

$this->dontReportCallbacks[] = $dontReportWhen;

return $this;
}

/**
* Indicate that the given exception type should not be reported.
*
Expand Down Expand Up @@ -413,6 +438,12 @@ protected function shouldntReport(Throwable $e)
return true;
}

foreach ($this->dontReportCallbacks as $dontReportCallback) {
if ($dontReportCallback($e) === true) {
return true;
}
}

return rescue(fn () => with($this->throttle($e), function ($throttle) use ($e) {
if ($throttle instanceof Unlimited || $throttle === null) {
return false;
Expand Down
23 changes: 23 additions & 0 deletions tests/Foundation/FoundationExceptionsHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,29 @@ 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->dontReportWhen(function (\Throwable $e) {
return $e->getMessage() === 'foo';
});

$this->handler->report($e1);
$this->handler->report($e2);
$this->handler->report($e1);

$this->assertSame($reported, [$e2]);
}

public function testItDoesNotThrottleExceptionsByDefault()
{
$reported = [];
Expand Down