Skip to content

Commit

Permalink
allow throttling of exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Nov 17, 2023
1 parent b7fcb73 commit 6f9810f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Illuminate/Foundation/Configuration/Exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ public function renderable(callable $renderUsing)
return $this;
}

/**
* Specify the callback that should be used to throttle reportable exceptions.
*
* @param callable $throttleUsing
* @return $this
*/
public function throttle(callable $throttleUsing)
{
$this->handler->throttleUsing($throttleUsing);

return $this;
}

/**
* Register a new exception mapping.
*
Expand Down
36 changes: 36 additions & 0 deletions src/Illuminate/Foundation/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ class Handler implements ExceptionHandlerContract
*/
protected $levels = [];

/**
* The callbacks that should be used to throttle reportable exceptions.
*
* @var array
*/
protected $throttleCallbacks = [];

/**
* The callbacks that should be used to build exception context data.
*
Expand Down Expand Up @@ -408,9 +415,38 @@ protected function shouldntReport(Throwable $e)
*/
protected function throttle(Throwable $e)
{
foreach ($this->throttleCallbacks as $throttleCallback) {
foreach ($this->firstClosureParameterTypes($throttleCallback) as $type) {
if (is_a($e, $type)) {
$response = $throttleCallback($e);

if (! is_null($response)) {
return $response;
}
}
}
}

return Limit::none();
}

/**
* Specify the callback that should be used to throttle reportable exceptions.
*
* @param callable $throttleUsing
* @return $this
*/
public function throttleUsing(callable $throttleUsing)
{
if (! $throttleUsing instanceof Closure) {
$throttleUsing = Closure::fromCallable($throttleUsing);
}

$this->throttleCallbacks[] = $throttleUsing;

return $this;
}

/**
* Remove the given exception class from the list of exceptions that should be ignored.
*
Expand Down

0 comments on commit 6f9810f

Please sign in to comment.