Skip to content

Commit 84544e2

Browse files
[10.x] Document throttling exceptions (#9053)
* Document throttling exceptions * exception throttling * wording --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 53d4cbe commit 84544e2

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

errors.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- [Ignoring Exceptions By Type](#ignoring-exceptions-by-type)
99
- [Rendering Exceptions](#rendering-exceptions)
1010
- [Reportable & Renderable Exceptions](#renderable-exceptions)
11+
- [Throttling Reported Exceptions](#throttling-reported-exceptions)
1112
- [HTTP Exceptions](#http-exceptions)
1213
- [Custom HTTP Error Pages](#custom-http-error-pages)
1314

@@ -316,6 +317,100 @@ If your exception contains custom reporting logic that is only necessary when ce
316317
> **Note**
317318
> You may type-hint any required dependencies of the `report` method and they will automatically be injected into the method by Laravel's [service container](/docs/{{version}}/container).
318319
320+
<a name="throttling-reported-exceptions"></a>
321+
### Throttling Reported Exceptions
322+
323+
If your application reports a very large number of exceptions, you may want to throttle how may exceptions are actually logged or sent to your application's external error tracking service.
324+
325+
To take a random sample rate of exceptions, you can return a `Lottery` instance from your exception handler's `throttle` method. If your `App\Exceptions\Handler` class does not contain this method, you may simply add it to the class:
326+
327+
```php
328+
use Illuminate\Support\Lottery;
329+
use Throwable;
330+
331+
/**
332+
* Throttle incoming exceptions.
333+
*/
334+
protected function throttle(Throwable $e): Lottery
335+
{
336+
return Lottery::odds(1, 1000);
337+
}
338+
```
339+
340+
It is also possible to conditionally sample based on the exception type. If you would like to only sample instances of a specific exception class, you may return a `Lottery` instance only for that class:
341+
342+
```php
343+
use App\Exceptions\ApiMonitoringException;
344+
use Throwable;
345+
346+
/**
347+
* Throttle incoming exceptions.
348+
*/
349+
protected function throttle(Throwable $e): Lottery
350+
{
351+
if ($e instanceof ApiMonitoringException) {
352+
return Lottery::odds(1, 1000);
353+
}
354+
}
355+
```
356+
357+
You may also rate limit exceptions logged or sent to an external error tracking service by returning a `Limit` instance instead of a `Lottery`. This is useful if you want to protect against sudden bursts of exceptions flooding your logs, for example, when a third-party service used by your application is down:
358+
359+
```php
360+
use Illuminate\Broadcasting\BroadcastException;
361+
use Illuminate\Cache\RateLimiting\Limit;
362+
use Throwable;
363+
364+
/**
365+
* Throttle incoming exceptions.
366+
*/
367+
protected function throttle(Throwable $e): mixed
368+
{
369+
if ($e instanceof BroadcastException) {
370+
return Limit::perMinute(300);
371+
}
372+
}
373+
```
374+
375+
By default, limits will use the exception's class as the rate limit key. You can customize this by specifying your own key using the `by` method on the `Limit`:
376+
377+
```php
378+
use Illuminate\Broadcasting\BroadcastException;
379+
use Illuminate\Cache\RateLimiting\Limit;
380+
use Throwable;
381+
382+
/**
383+
* Throttle incoming exceptions.
384+
*/
385+
protected function throttle(Throwable $e): mixed
386+
{
387+
if ($e instanceof BroadcastException) {
388+
return Limit::perMinute(300)->by($e->getMessage());
389+
}
390+
}
391+
```
392+
393+
Of course, you may return a mixture of `Lottery` and `Limit` instances for different exceptions:
394+
395+
```php
396+
use App\Exceptions\ApiMonitoringException;
397+
use Illuminate\Broadcasting\BroadcastException;
398+
use Illuminate\Cache\RateLimiting\Limit;
399+
use Throwable;
400+
401+
/**
402+
* Throttle incoming exceptions.
403+
*/
404+
protected function throttle(Throwable $e): mixed
405+
{
406+
return match (true) {
407+
$e instanceof BroadcastException => Limit::perMinute(300),
408+
$e instanceof ApiMonitoringException => Lottery::odds(1, 1000),
409+
default => Limit::none(),
410+
};
411+
}
412+
```
413+
319414
<a name="http-exceptions"></a>
320415
## HTTP Exceptions
321416

0 commit comments

Comments
 (0)