Skip to content

Commit

Permalink
Handle expiration in seconds (#48600)
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald authored Oct 2, 2023
1 parent cd68b64 commit 2bfb70b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Illuminate/Foundation/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ protected function shouldntReport(Throwable $e)
with($throttle->key ?: 'illuminate:foundation:exceptions:'.$e::class, fn ($key) => $this->hashThrottleKeys ? md5($key) : $key),
$throttle->maxAttempts,
fn () => true,
$throttle->decayMinutes
60 * $throttle->decayMinutes
);
}), rescue: false, report: false);
}
Expand Down
46 changes: 46 additions & 0 deletions tests/Foundation/FoundationExceptionsHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,52 @@ public function attempt($key, $maxAttempts, Closure $callback, $decaySeconds = 6
$this->assertCount(14, $reported);
$this->assertSame('Something in the app went wrong.', $reported[0]->getMessage());
}

public function testRateLimitExpiresOnBoundary()
{
$handler = new class($this->container) extends Handler
{
protected function throttle($e)
{
return Limit::perMinute(1);
}
};
$reported = [];
$handler->reportable(function (\Throwable $e) use (&$reported) {
$reported[] = $e;

return false;
});
$this->container->instance(RateLimiter::class, $limiter = new class(new Repository(new ArrayStore)) extends RateLimiter
{
public $attempted = 0;

public function attempt($key, $maxAttempts, Closure $callback, $decaySeconds = 60)
{
$this->attempted++;

return parent::attempt(...func_get_args());
}
});

Carbon::setTestNow('2000-01-01 00:00:00.000');
$handler->report(new Exception('Something in the app went wrong 1.'));
Carbon::setTestNow('2000-01-01 00:00:59.999');
$handler->report(new Exception('Something in the app went wrong 1.'));

$this->assertSame(2, $limiter->attempted);
$this->assertCount(1, $reported);
$this->assertSame('Something in the app went wrong 1.', $reported[0]->getMessage());

Carbon::setTestNow('2000-01-01 00:01:00.000');
$handler->report(new Exception('Something in the app went wrong 2.'));
Carbon::setTestNow('2000-01-01 00:01:59.999');
$handler->report(new Exception('Something in the app went wrong 2.'));

$this->assertSame(4, $limiter->attempted);
$this->assertCount(2, $reported);
$this->assertSame('Something in the app went wrong 2.', $reported[1]->getMessage());
}
}

class CustomException extends Exception
Expand Down

0 comments on commit 2bfb70b

Please sign in to comment.