|
8 | 8 | - [Ignoring Exceptions By Type](#ignoring-exceptions-by-type)
|
9 | 9 | - [Rendering Exceptions](#rendering-exceptions)
|
10 | 10 | - [Reportable & Renderable Exceptions](#renderable-exceptions)
|
| 11 | +- [Throttling Reported Exceptions](#throttling-reported-exceptions) |
11 | 12 | - [HTTP Exceptions](#http-exceptions)
|
12 | 13 | - [Custom HTTP Error Pages](#custom-http-error-pages)
|
13 | 14 |
|
@@ -316,6 +317,100 @@ If your exception contains custom reporting logic that is only necessary when ce
|
316 | 317 | > **Note**
|
317 | 318 | > 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).
|
318 | 319 |
|
| 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 | + |
319 | 414 | <a name="http-exceptions"></a>
|
320 | 415 | ## HTTP Exceptions
|
321 | 416 |
|
|
0 commit comments