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

docs: update to L9 method of error exception handling #27

Merged
merged 1 commit into from
Jun 10, 2022
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
35 changes: 17 additions & 18 deletions exception-handler.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,15 @@ class Handler extends ExceptionHandler
];

/**
* Report or log an exception.
* Register the exception handling callbacks for the application.
*
* @param \Throwable $exception
* @return void
*
* @throws \Exception
*/
public function report(Throwable $exception)
public function register()
{
parent::report($exception);
$this->reportable(function (Throwable $e) {
//
});
}
}
```
Expand All @@ -65,6 +64,8 @@ As you can see we are just extending the default handler here.
> The above content might feel familiar to those using Laravel.
It's a trimmed down version of the [`App\Exceptions\Handler`](https://github.com/laravel/laravel/blob/master/app/Exceptions/Handler.php) of Laravel.

See [the official Laravel documentation](https://laravel.com/docs/errors#reporting-exceptions) for more details.

<a name="replacing-the-default-handler"></a>
#### Replacing The Default Handler

Expand Down Expand Up @@ -99,31 +100,29 @@ protected $dontReport = [
];
```

This however results in `RuntimeException` not being reported at all. More fine grained control can be achieved by updating the `report` function.
This however results in `RuntimeException` not being reported at all. More fine grained control can be achieved by updating the `register` function.

The following would prevent the *Not enough arguments* exception to be reported, but any other `RuntimeException` would still be reported.

```php
public function report(Throwable $exception)
public function register(Throwable $exception)
{
if ($exception instanceof RuntimeException) {
if (Str::contains($exception->getMessage(), ['Not enough arguments'])) {
return;
$this->reportable(function (RuntimeException $e) {
if (Str::contains($e->getMessage(), ['Not enough arguments'])) {
return false;
}
}

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

If you would like to disable the reporting only when running from the built phar file you could use the [`\Phar::running()`](https://php.net/manual/en/phar.running.php) function.

```php
if (\Phar::running()) {
if ($exception instanceof RuntimeException) {
if (Str::contains($exception->getMessage(), ['Not enough arguments'])) {
return;
$this->reportable(function (RuntimeException $e) {
if (Str::contains($e->getMessage(), ['Not enough arguments'])) {
return false;
}
}
});
}
```