From c5ff3ca4797cb5752f4439845d97b584b1fcea96 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Thu, 14 Sep 2023 00:44:46 +1000 Subject: [PATCH] Document deduplicating exceptions (#9026) * Document deduplicating exceptions * formatting * formatting --------- Co-authored-by: Taylor Otwell --- errors.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/errors.md b/errors.md index 6c356ff8ae4..288a19f030d 100644 --- a/errors.md +++ b/errors.md @@ -117,6 +117,42 @@ Sometimes you may need to report an exception but continue handling the current } } + +#### Deduplicating Reported Exceptions + +If you are using the `report` function throughout your application, you may occasionally report the same exception multiple times, creating duplicate entries in your logs. + +If you would like to ensure that a single instance of an exception is only ever reported once, you may call the exception handler's `dontReportDuplicates` method. Typically, this method should be invoked from the `boot` method of your application's `AppServiceProvider`: + +```php +use Illuminate\Contracts\Debug\ExceptionHandler; + +/** + * Bootstrap any application services. + */ +public function boot(ExceptionHandler $exceptionHandler): void +{ + $exceptionHandler->dontReportDuplicates(); +} +``` + +Now, when the `report` helper is called with the same instance of an exception, only the first call will be reported: + +```php +$original = new RuntimeException('Whoops!'); + +report($original); // reported + +try { + throw $original; +} catch (Throwable $caught) { + report($caught); // ignored +} + +report($original); // ignored +report($caught); // ignored +``` + ### Exception Log Levels