Skip to content

Commit

Permalink
Prevent the same exception from being sent to Scout multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
asgrim committed Mar 23, 2022
1 parent 7805954 commit 8f64267
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
13 changes: 12 additions & 1 deletion src/Errors/ScoutErrorHandling.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use function is_array;
use function register_shutdown_function;
use function set_exception_handler;
use function spl_object_hash;

use const E_COMPILE_ERROR;
use const E_CORE_ERROR;
Expand All @@ -36,6 +37,8 @@ final class ScoutErrorHandling implements ErrorHandling
private $reportingClient;
/** @var Config */
private $config;
/** @var list<string> */
private $objectHashesOfHandledExceptions = [];
/** @var list<ErrorEvent> */
private $errorEvents = [];
/** @var callable|null */
Expand Down Expand Up @@ -102,7 +105,15 @@ public function recordThrowable(Throwable $throwable): void
return;
}

$this->errorEvents[] = ErrorEvent::fromThrowable($this->request, $throwable);
$thisThrowableObjectHash = spl_object_hash($throwable);

// Storing the object hashes & checking means we don't send exactly the same exception twice in one request
if (! in_array($thisThrowableObjectHash, $this->objectHashesOfHandledExceptions, true)) {
$this->objectHashesOfHandledExceptions[] = $thisThrowableObjectHash;

$this->errorEvents[] = ErrorEvent::fromThrowable($this->request, $throwable);
}

$this->sendCollectedErrors();
}

Expand Down
20 changes: 18 additions & 2 deletions tests/Unit/Errors/ScoutErrorHandlingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,27 @@ public function testExceptionIsNotRecordedWhenDisabled(): void

$this->reportingClient
->expects(self::never())
->method('sendErrorToScout')
->with(self::isInstanceOf(ErrorEvent::class));
->method('sendErrorToScout');

$handling->recordThrowable(new RuntimeException());

$handling->sendCollectedErrors();
}

public function testExactlyTheSameExceptionIsNotSentTwice(): void
{
$handling = $this->errorHandlingFromConfig(Config::fromArray([Config\ConfigKey::ERRORS_ENABLED => true]));

$this->reportingClient
->expects(self::once())
->method('sendErrorToScout')
->with(self::containsOnlyInstancesOf(ErrorEvent::class));

$exceptionInstance = new RuntimeException();

$handling->recordThrowable($exceptionInstance);
$handling->recordThrowable($exceptionInstance);

$handling->sendCollectedErrors();
}
}

0 comments on commit 8f64267

Please sign in to comment.