Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reporter for Model::preventAccessingMissingAttributes() #824

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion src/Sentry/Laravel/Integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Sentry\Laravel;

use Illuminate\Database\Eloquent\MissingAttributeException;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\LazyLoadingViolationException;
use Illuminate\Routing\Route;
Expand Down Expand Up @@ -215,6 +216,55 @@ public static function captureUnhandledException(Throwable $throwable): ?EventId
return SentrySdk::getCurrentHub()->captureException($throwable, $hint);
}

/**
* Returns a callback that can be passed to `Model::handleMissingAttributeViolationUsing` to report missing attribute violations to Sentry.
*
* @param callable|null $callback Optional callback to be called after the violation is reported to Sentry.
*
* @return callable
*/
public static function missingAttributeViolationReporter(?callable $callback = null): callable
{
return new class($callback) {
use ResolvesEventOrigin;

/** @var callable|null $callback */
private $callback;

public function __construct(?callable $callback)
{
$this->callback = $callback;
}

public function __invoke(Model $model, string $attribute): void
{
SentrySdk::getCurrentHub()->withScope(function (Scope $scope) use ($model, $attribute) {
$scope->setContext('violation', [
'model' => get_class($model),
'attribute' => $attribute,
'origin' => $this->resolveEventOrigin(),
'kind' => 'missing_attribute',
]);

SentrySdk::getCurrentHub()->captureEvent(
tap(Event::createEvent(), static function (Event $event) {
$event->setLevel(Severity::warning());
}),
EventHint::fromArray([
'exception' => new MissingAttributeException($model, $attribute),
'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, $attribute);
}
}
};
}

/**
* Returns a callback that can be passed to `Model::handleLazyLoadingViolationUsing` to report lazy loading violations to Sentry.
*
Expand All @@ -238,7 +288,7 @@ public function __construct(?callable $callback)
public function __invoke(Model $model, string $relation): void
{
// 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#L563
// See: https://github.com/laravel/framework/blob/438d02d3a891ab4d73ffea2c223b5d37947b5e93/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php#L559-L561
if (!$model->exists || $model->wasRecentlyCreated) {
return;
}
Expand All @@ -248,6 +298,7 @@ public function __invoke(Model $model, string $relation): void
'model' => get_class($model),
'relation' => $relation,
'origin' => $this->resolveEventOrigin(),
'kind' => 'lazy_loading',
]);

SentrySdk::getCurrentHub()->captureEvent(
Expand Down
Loading