-
-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1c1c1ca
commit 0abf38b
Showing
4 changed files
with
138 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/Sentry/Laravel/Integration/ModelViolations/LazyLoadingModelViolationReporter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Sentry\Laravel\Integration\ModelViolations; | ||
|
||
use Exception; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\LazyLoadingViolationException; | ||
|
||
class LazyLoadingModelViolationReporter extends ModelViolationReporter | ||
{ | ||
protected function getViolationContext(Model $model, string $property): array | ||
{ | ||
return [ | ||
'relation' => $property, | ||
'kind' => 'lazy_loading', | ||
]; | ||
} | ||
|
||
protected function getViolationException(Model $model, string $property): Exception | ||
{ | ||
return new LazyLoadingViolationException($model, $property); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/Sentry/Laravel/Integration/ModelViolations/MissingAttributeModelViolationReporter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Sentry\Laravel\Integration\ModelViolations; | ||
|
||
use Exception; | ||
use Illuminate\Database\Eloquent\MissingAttributeException; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class MissingAttributeModelViolationReporter extends ModelViolationReporter | ||
{ | ||
protected function getViolationContext(Model $model, string $property): array | ||
{ | ||
return [ | ||
'attribute' => $property, | ||
'kind' => 'missing_attribute', | ||
]; | ||
} | ||
|
||
protected function getViolationException(Model $model, string $property): Exception | ||
{ | ||
return new MissingAttributeException($model, $property); | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
src/Sentry/Laravel/Integration/ModelViolations/ModelViolationReporter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
namespace Sentry\Laravel\Integration\ModelViolations; | ||
|
||
use Exception; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Sentry\Event; | ||
use Sentry\EventHint; | ||
use Sentry\ExceptionMechanism; | ||
use Sentry\Laravel\Features\Concerns\ResolvesEventOrigin; | ||
use Sentry\SentrySdk; | ||
use Sentry\Severity; | ||
use Sentry\State\Scope; | ||
|
||
abstract class ModelViolationReporter | ||
{ | ||
use ResolvesEventOrigin; | ||
|
||
/** @var callable|null $callback */ | ||
private $callback; | ||
|
||
/** @var bool $supressDuplicateReports */ | ||
private $supressDuplicateReports; | ||
|
||
/** @var array<string, true> $reportedViolations */ | ||
private $reportedViolations = []; | ||
|
||
public function __construct(?callable $callback, bool $supressDuplicateReports) | ||
{ | ||
$this->callback = $callback; | ||
$this->supressDuplicateReports = $supressDuplicateReports; | ||
} | ||
|
||
public function __invoke(Model $model, string $property): void | ||
{ | ||
if ($this->hasAlreadyBeenReported($model, $property)) { | ||
return; | ||
} | ||
|
||
$this->markAsReported($model, $property); | ||
|
||
SentrySdk::getCurrentHub()->withScope(function (Scope $scope) use ($model, $property) { | ||
$scope->setContext('violation', array_merge([ | ||
'model' => get_class($model), | ||
'origin' => $this->resolveEventOrigin(), | ||
], $this->getViolationContext($model, $property))); | ||
|
||
SentrySdk::getCurrentHub()->captureEvent( | ||
tap(Event::createEvent(), static function (Event $event) { | ||
$event->setLevel(Severity::warning()); | ||
}), | ||
EventHint::fromArray([ | ||
'exception' => $this->getViolationException($model, $property), | ||
'mechanism' => new ExceptionMechanism(ExceptionMechanism::TYPE_GENERIC, true), | ||
]) | ||
); | ||
}); | ||
|
||
// Forward the violation to the next handler if there is one | ||
if ($this->callback !== null) { | ||
call_user_func($this->callback, $model, $property); | ||
} | ||
} | ||
|
||
abstract protected function getViolationContext(Model $model, string $property): array; | ||
|
||
abstract protected function getViolationException(Model $model, string $property): Exception; | ||
|
||
private function hasAlreadyBeenReported(Model $model, string $property): bool | ||
{ | ||
if (!$this->supressDuplicateReports) { | ||
return false; | ||
} | ||
|
||
return array_key_exists(get_class($model) . $property, $this->reportedViolations); | ||
} | ||
|
||
private function markAsReported(Model $model, string $property): void | ||
{ | ||
if (!$this->supressDuplicateReports) { | ||
return; | ||
} | ||
|
||
$this->reportedViolations[get_class($model) . $property] = true; | ||
} | ||
} |