Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor model violation reporters (#825)
* Add reporter for `Model::preventAccessingMissingAttributes()` * Refactor model violation reporters * Add `discardedAttributeViolationReporter` * Delay sending to after the app has terminated * Fix reporting the origin when sending delayed report * Add some basic tests * Fix tests on older Laravel versions
- Loading branch information
1 parent
300bb6f
commit fefacb9
Showing
6 changed files
with
281 additions
and
90 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
27 changes: 27 additions & 0 deletions
27
src/Sentry/Laravel/Integration/ModelViolations/DiscardedAttributeViolationReporter.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,27 @@ | ||
<?php | ||
|
||
namespace Sentry\Laravel\Integration\ModelViolations; | ||
|
||
use Exception; | ||
use Illuminate\Database\Eloquent\MassAssignmentException; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class DiscardedAttributeViolationReporter extends ModelViolationReporter | ||
{ | ||
protected function getViolationContext(Model $model, string $property): array | ||
{ | ||
return [ | ||
'attribute' => $property, | ||
'kind' => 'discarded_attribute', | ||
]; | ||
} | ||
|
||
protected function getViolationException(Model $model, string $property): Exception | ||
{ | ||
return new MassAssignmentException(sprintf( | ||
'Add [%s] to fillable property to allow mass assignment on [%s].', | ||
$property, | ||
get_class($model) | ||
)); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
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,34 @@ | ||
<?php | ||
|
||
namespace Sentry\Laravel\Integration\ModelViolations; | ||
|
||
use Exception; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\LazyLoadingViolationException; | ||
|
||
class LazyLoadingModelViolationReporter extends ModelViolationReporter | ||
{ | ||
protected function shouldReport(Model $model, string $property): bool | ||
{ | ||
// Laravel uses these checks itself to not throw an exception if the model doesn't exist or was just created | ||
// See: https://github.com/laravel/framework/blob/438d02d3a891ab4d73ffea2c223b5d37947b5e93/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php#L559-L561 | ||
if (!$model->exists || $model->wasRecentlyCreated) { | ||
return false; | ||
} | ||
|
||
return parent::shouldReport($model, $property); | ||
} | ||
|
||
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); | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
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,103 @@ | ||
<?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 $suppressDuplicateReports */ | ||
private $suppressDuplicateReports; | ||
|
||
/** @var bool $reportAfterResponse */ | ||
private $reportAfterResponse; | ||
|
||
/** @var array<string, true> $reportedViolations */ | ||
private $reportedViolations = []; | ||
|
||
public function __construct(?callable $callback, bool $suppressDuplicateReports, bool $reportAfterResponse) | ||
{ | ||
$this->callback = $callback; | ||
$this->suppressDuplicateReports = $suppressDuplicateReports; | ||
$this->reportAfterResponse = $reportAfterResponse; | ||
} | ||
|
||
public function __invoke(Model $model, string $property): void | ||
{ | ||
if (!$this->shouldReport($model, $property)) { | ||
return; | ||
} | ||
|
||
$this->markAsReported($model, $property); | ||
|
||
$origin = $this->resolveEventOrigin(); | ||
|
||
if ($this->reportAfterResponse) { | ||
app()->terminating(function () use ($model, $property, $origin) { | ||
$this->report($model, $property, $origin); | ||
}); | ||
} else { | ||
$this->report($model, $property, $origin); | ||
} | ||
} | ||
|
||
abstract protected function getViolationContext(Model $model, string $property): array; | ||
|
||
abstract protected function getViolationException(Model $model, string $property): Exception; | ||
|
||
protected function shouldReport(Model $model, string $property): bool | ||
{ | ||
if (!$this->suppressDuplicateReports) { | ||
return true; | ||
} | ||
|
||
return !array_key_exists(get_class($model) . $property, $this->reportedViolations); | ||
} | ||
|
||
protected function markAsReported(Model $model, string $property): void | ||
{ | ||
if (!$this->suppressDuplicateReports) { | ||
return; | ||
} | ||
|
||
$this->reportedViolations[get_class($model) . $property] = true; | ||
} | ||
|
||
private function report(Model $model, string $property, $origin): void | ||
{ | ||
SentrySdk::getCurrentHub()->withScope(function (Scope $scope) use ($model, $property, $origin) { | ||
$scope->setContext('violation', array_merge([ | ||
'model' => get_class($model), | ||
'origin' => $origin, | ||
], $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); | ||
} | ||
} | ||
} |
Oops, something went wrong.