Skip to content

Commit

Permalink
Add support for overriding grouping
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche committed Dec 2, 2024
1 parent 3689bd8 commit b8e8b56
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/Enums/OverriddenGrouping.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Spatie\FlareClient\Enums;

class OverriddenGrouping
{
const ExceptionClass = 'exception_class';
const ExceptionMessage = 'exception_message';
const ExceptionMessageAndClass = 'exception_message_and_class';
}
20 changes: 19 additions & 1 deletion src/Flare.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Spatie\FlareClient\Context\BaseContextProviderDetector;
use Spatie\FlareClient\Context\ContextProviderDetector;
use Spatie\FlareClient\Enums\MessageLevels;
use Spatie\FlareClient\Enums\OverriddenGrouping;
use Spatie\FlareClient\FlareMiddleware\AddEnvironmentInformation;
use Spatie\FlareClient\FlareMiddleware\AddGlows;
use Spatie\FlareClient\FlareMiddleware\CensorRequestBodyFields;
Expand Down Expand Up @@ -68,6 +69,9 @@ class Flare

protected bool $withStackFrameArguments = true;

/** @var array<class-string, string> */
protected array $overriddenGroupings = [];

public static function make(
?string $apiKey = null,
?ContextProviderDetector $contextDetector = null
Expand Down Expand Up @@ -159,6 +163,19 @@ public function withStackFrameArguments(
return $this;
}

/**
* @param class-string $exceptionClass
*/
public function overrideGrouping(
string $exceptionClass,
string $type = OverriddenGrouping::ExceptionMessageAndClass,
): self
{
$this->overriddenGroupings[$exceptionClass] = $type;

return $this;
}

public function version(): ?string
{
if (! $this->determineVersionCallable) {
Expand Down Expand Up @@ -437,7 +454,8 @@ public function createReport(Throwable $throwable): Report
$this->applicationPath,
$this->version(),
$this->argumentReducers,
$this->withStackFrameArguments
$this->withStackFrameArguments,
$this->overriddenGroupings,
);

return $this->applyMiddlewareToReport($report);
Expand Down
23 changes: 21 additions & 2 deletions src/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class Report

protected ?bool $handled = null;

protected ?string $overriddenGrouping = null;

/** @param array<class-string<ArgumentReducer>|ArgumentReducer>|ArgumentReducers|null $argumentReducers */
public static function createForThrowable(

Check failure on line 75 in src/Report.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Spatie\FlareClient\Report::createForThrowable() has parameter $overriddenGroupings with no value type specified in iterable type array.
Throwable $throwable,
Expand All @@ -77,21 +79,30 @@ public static function createForThrowable(
?string $version = null,
null|array|ArgumentReducers $argumentReducers = null,
bool $withStackTraceArguments = true,
array $overriddenGroupings = [],
): self {
$stacktrace = Backtrace::createForThrowable($throwable)
->withArguments($withStackTraceArguments)
->reduceArguments($argumentReducers)
->applicationPath($applicationPath ?? '');

return (new self())
$exceptionClass = self::getClassForThrowable($throwable);

$report = (new self())
->setApplicationPath($applicationPath)
->throwable($throwable)
->useContext($context)
->exceptionClass(self::getClassForThrowable($throwable))
->exceptionClass($exceptionClass)
->message($throwable->getMessage())
->stackTrace($stacktrace)
->exceptionContext($throwable)
->setApplicationVersion($version);

if (array_key_exists($exceptionClass, $overriddenGroupings)) {
$report->overriddenGrouping($overriddenGroupings[$exceptionClass]);
}

return $report;
}

protected static function getClassForThrowable(Throwable $throwable): string
Expand Down Expand Up @@ -311,6 +322,13 @@ public function handled(?bool $handled = true): self
return $this;
}

public function overriddenGrouping(?string $overriddenGrouping): self
{
$this->overriddenGrouping = $overriddenGrouping;

return $this;
}

protected function exceptionContext(Throwable $throwable): self
{
if ($throwable instanceof ProvidesFlareContext) {
Expand Down Expand Up @@ -388,6 +406,7 @@ public function toArray(): array
'application_version' => $this->applicationVersion,
'tracking_uuid' => $this->trackingUuid,
'handled' => $this->handled,
'overridden_grouping' => $this->overriddenGrouping,
];
}

Expand Down
18 changes: 18 additions & 0 deletions tests/FlareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use PHPUnit\Framework\Exception;
use Spatie\FlareClient\Enums\MessageLevels;
use Spatie\FlareClient\Enums\OverriddenGrouping;
use Spatie\FlareClient\Flare;
use Spatie\FlareClient\Tests\Concerns\MatchesReportSnapshots;
use Spatie\FlareClient\Tests\Mocks\FakeClient;
Expand Down Expand Up @@ -362,6 +363,23 @@
expect($report['handled'])->toBeTrue();
});

it('can override the grouping algorithm for specific classes', function (){
$throwable = new RuntimeException('This is a test');

$this->flare->overrideGrouping(
RuntimeException::class,
OverriddenGrouping::ExceptionMessageAndClass
);

$this->flare->reportHandled($throwable);

$this->fakeClient->assertRequestsSent(1);

$report = $this->fakeClient->getLastPayload();

expect($report['overridden_grouping'])->toBe('exception_message_and_class');
});

// Helpers
function reportException()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ application_path: null
application_version: null
tracking_uuid: fake-uuid
handled: null
overridden_grouping: null
uuid: fake-uuid
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ application_path: null
application_version: null
tracking_uuid: fake-uuid
handled: null
overridden_grouping: null
uuid: fake-uuid
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ application_path: null
application_version: null
tracking_uuid: fake-uuid
handled: null
overridden_grouping: null
uuid: fake-uuid
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ application_path: null
application_version: null
tracking_uuid: fake-uuid
handled: null
overridden_grouping: null
uuid: fake-uuid
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ application_path: null
application_version: null
tracking_uuid: fake-uuid
handled: null
overridden_grouping: null
uuid: fake-uuid

0 comments on commit b8e8b56

Please sign in to comment.