Skip to content

Commit

Permalink
Add discardedAttributeViolationReporter
Browse files Browse the repository at this point in the history
  • Loading branch information
stayallive committed Apr 11, 2024
1 parent ba4fdac commit 70d2134
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/Sentry/Laravel/Integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
use Sentry\EventHint;
use Sentry\EventId;
use Sentry\ExceptionMechanism;
use Sentry\Laravel\Integration\ModelViolations\LazyLoadingModelViolationReporter;
use Sentry\Laravel\Integration\ModelViolations\MissingAttributeModelViolationReporter;
use Sentry\Laravel\Integration\ModelViolations as ModelViolationReports;
use Sentry\SentrySdk;
use Sentry\Tracing\TransactionSource;
use Throwable;
Expand Down Expand Up @@ -248,7 +247,7 @@ public static function captureUnhandledException(Throwable $throwable): ?EventId
*/
public static function missingAttributeViolationReporter(?callable $callback = null, bool $suppressDuplicateReports = true): callable
{
return new MissingAttributeModelViolationReporter($callback, $suppressDuplicateReports);
return new ModelViolationReports\MissingAttributeModelViolationReporter($callback, $suppressDuplicateReports);
}

/**
Expand All @@ -261,7 +260,20 @@ public static function missingAttributeViolationReporter(?callable $callback = n
*/
public static function lazyLoadingViolationReporter(?callable $callback = null, bool $suppressDuplicateReports = true): callable
{
return new LazyLoadingModelViolationReporter($callback, $suppressDuplicateReports);
return new ModelViolationReports\LazyLoadingModelViolationReporter($callback, $suppressDuplicateReports);
}

/**
* Returns a callback that can be passed to `Model::handleDiscardedAttributeViolationUsing` to report discarded attribute violations to Sentry.
*
* @param callable|null $callback Optional callback to be called after the violation is reported to Sentry.
* @param bool $suppressDuplicateReports Whether to suppress duplicate reports of the same violation.
*
* @return callable
*/
public static function discardedAttributeViolationReporter(?callable $callback = null, bool $suppressDuplicateReports = true): callable
{
return new ModelViolationReports\DiscardedAttributeViolationReporter($callback, $suppressDuplicateReports);
}

/**
Expand Down
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)
));
}
}

0 comments on commit 70d2134

Please sign in to comment.