Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/abenevaut/laravel-sentry-handler

Package that facilitates sentry integration with context scoped exceptions that are able to transport data when an exception happened. [READONLY]
https://github.com/abenevaut/laravel-sentry-handler

crash-reporting laravel sentry

Last synced: 7 days ago
JSON representation

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

Awesome Lists containing this project

README

        

# laravel-sentry-handler

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

## Install
```shell
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:
```php
public function report(\Throwable $e): void
{
// Report standard exceptions to sentry
$this->reportSentry($e);

parent::report($e);
}
```

Note: that method is used in [demo](https://github.com/abenevaut/demo-laravel-sentry-handler)

#### Handler Trait

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

Then adjust your `report()` method:
```php
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.

```php
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
```php
// incoming soon
```

- Send Sentry message
```php
// incoming soon
```

### What a scope

```php
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
```shell
vendor/bin/smelly-code-detector inspect src
vendor/bin/phpunit
```