abenevaut/laravel-sentry-handler

Laravel sentry exceptions handler

1.0.2 2024-04-14 12:02 UTC

This package is auto-updated.

Last update: 2024-11-14 13:11:40 UTC


README

Package that facilitates sentry integration with context scoped exceptions that are able to transport data when an exception happened.

Install

composer require abenevaut/laravel-sentry-handler
php artisan sentry:publish --dsn=

Usage

Update ExceptionHandler

Scoped Exception vendorized in this package are able to report themself to Sentry. Because we probably want to report all exceptions to Sentry, we are able to implement $this->reportSentry($e); to record them to Sentry.

Inherited Handler

In app/Exceptions/Handler.php, replace use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; by use abenevaut\SentryHandler\Handler as ExceptionHandler;

If you already customized your exception handler, be sure to adjust your report() method:

public function report(\Throwable $e): void
{
    // Report standard exceptions to sentry
    $this->reportSentry($e);

    parent::report($e);
}

Note: that method is used in demo

Handler Trait

In app/Exceptions/Handler.php, add use SentryHandlerTrait; in App\Exceptions\Handler class.

Then adjust your report() method:

public function report(\Throwable $e): void
{
    // Report standard exceptions to sentry
    $this->reportSentry($e);

    parent::report($e);
}

Test Sentry with standard exceptions

php artisan sentry:test

Scoped Exceptions

Laravel ExceptionHandler allows an exception to report herself by implementing report() method. We use that place to compute exception context and then throw it to Sentry.

final class MyException extends \abenevaut\SentryHandler\Contracts\ExceptionAbstract
{
    /**
     * @var array|string[]
     */
    private array $scopes = [
        /*
         * Context always reported
         */
        DefaultScope::class,
    ];
} 

$exception = new MyException();

// Depending context, add relative scope
$exception->addScope( DefaultScope::class );
// You can also pass an instantiated object, if you required to compute something
$exception->addScope( new DefaultScope( ... ) );

report($exception);
  • Set exception severity
// incoming soon
  • Send Sentry message
// incoming soon

What a scope

final class DefaultScope extends \abenevaut\SentryHandler\Contracts\ScopeAbstract
{
    public function handle(Scope $scope, Closure $next)
    {
        /*
         * Stack context in Sentry scope.
         * @seealso https://docs.sentry.io/platforms/php/guides/laravel/enriching-events/?original_referrer=https%3A%2F%2Fwww.google.com%2F
         */
        $scope
            ->setUser([
                // ...
            ])
            ->setTags([
                // ...
            ]);

        return $next($scope);
    }
}

Tests

vendor/bin/smelly-code-detector inspect src
vendor/bin/phpunit