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
35 changes: 35 additions & 0 deletions src/Illuminate/Foundation/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Throwable;
use WeakMap;

class Handler implements ExceptionHandlerContract
{
Expand Down Expand Up @@ -119,6 +120,20 @@ class Handler implements ExceptionHandlerContract
'password_confirmation',
];

/**
* Indicates that exception reporting should be deduplicated.
*
* @var bool
*/
protected $deduplicateReporting = false;

/**
* The already reported exception map.
*
* @var \WeakMap
*/
protected $reportedExceptionMap;

/**
* Create a new exception handler instance.
*
Expand All @@ -129,6 +144,8 @@ public function __construct(Container $container)
{
$this->container = $container;

$this->reportedExceptionMap = new WeakMap;

$this->register();
}

Expand Down Expand Up @@ -260,6 +277,8 @@ public function report(Throwable $e)
*/
protected function reportThrowable(Throwable $e): void
{
$this->reportedExceptionMap[$e] = true;

if (Reflector::isCallable($reportCallable = [$e, 'report']) &&
$this->container->call($reportCallable) !== false) {
return;
Expand Down Expand Up @@ -307,6 +326,10 @@ public function shouldReport(Throwable $e)
*/
protected function shouldntReport(Throwable $e)
{
if ($this->deduplicateReporting && ($this->reportedExceptionMap[$e] ?? false)) {
return true;
}

$dontReport = array_merge($this->dontReport, $this->internalDontReport);

return ! is_null(Arr::first($dontReport, fn ($type) => $e instanceof $type));
Expand Down Expand Up @@ -786,6 +809,18 @@ public function renderForConsole($output, Throwable $e)
(new ConsoleApplication)->renderThrowable($e, $output);
}

/**
* Do not report duplicate exceptions.
*
* @return $this
*/
public function dontReportDuplicates()
{
$this->deduplicateReporting = true;

return $this;
}

/**
* Determine if the given exception is an HTTP exception.
*
Expand Down
34 changes: 34 additions & 0 deletions tests/Foundation/FoundationExceptionsHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,40 @@ public function testAssertExceptionIsThrown()
Assert::fail('assertThrows failed: non matching message are thrown.');
}
}

public function testItReportsDuplicateExceptions()
{
$reported = [];
$this->handler->reportable(function (\Throwable $e) use (&$reported) {
$reported[] = $e;

return false;
});

$this->handler->report($one = new RuntimeException('foo'));
$this->handler->report($one);
$this->handler->report($two = new RuntimeException('foo'));

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

public function testItCanDedupeExceptions()
{
$reported = [];
$e = new RuntimeException('foo');
$this->handler->reportable(function (\Throwable $e) use (&$reported) {
$reported[] = $e;

return false;
});

$this->handler->dontReportDuplicates();
$this->handler->report($one = new RuntimeException('foo'));
$this->handler->report($one);
$this->handler->report($two = new RuntimeException('foo'));

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

class CustomException extends Exception
Expand Down