Skip to content

Commit

Permalink
Catch Predis\ClientException when no Sentinels are available (#31)
Browse files Browse the repository at this point in the history
When "No sentinel server available for autodiscovery" happens, `FailureHandlingThrottler` should catch this.

Co-authored-by: Valentinas Bartusevičius <[email protected]>
  • Loading branch information
vbartusevicius and Valentinas Bartusevičius authored Mar 11, 2021
1 parent 512856a commit 801b950
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
15 changes: 12 additions & 3 deletions src/Service/RedisFailureHandler/FailureHandlingThrottler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Maba\Bundle\GentleForceBundle\Service\RedisFailureHandler;

use Exception;
use Maba\GentleForce\IncreaseResult;
use Maba\GentleForce\RateLimitProvider;
use Maba\GentleForce\ThrottlerInterface;
use Predis\ClientException;
use Predis\Connection\ConnectionException;
use Psr\Log\LoggerInterface;

Expand All @@ -29,9 +31,9 @@ public function checkAndIncrease($useCaseKey, $identifier)
try {
return $this->throttler->checkAndIncrease($useCaseKey, $identifier);
} catch (ConnectionException $exception) {
$this->logger->error('Connection to redis failed while checking rate limits', [
'exception' => $exception,
]);
$this->logException($exception);
} catch (ClientException $clientException) {
$this->logException($clientException);
}

$usagesAvailable = 0;
Expand All @@ -51,4 +53,11 @@ public function reset($useCaseKey, $identifier)
{
return $this->throttler->reset($useCaseKey, $identifier);
}

private function logException(Exception $exception)
{
$this->logger->error('Connection to redis failed while checking rate limits', [
'exception' => $exception,
]);
}
}
15 changes: 11 additions & 4 deletions tests/Functional/FunctionalRecaptchaTemplateStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,32 @@ public function testRecaptchaTemplateStrategy()
$request = $this->createRequest(self::PATH_API1, self::DEFAULT_IP);
$request->setMethod('POST');
$response = $this->handleRequest($request);
$this->assertStringContainsString(
$this->checkIfStringExists(
'<html',
$response->getContent(),
'Expected response to contain <html> tag'
);
$this->assertStringContainsString(
$this->checkIfStringExists(
'my_recaptcha_site_key',
$response->getContent(),
'Expected response to contain recaptcha site key'
);
$this->assertStringContainsString(
$this->checkIfStringExists(
'/prefix/gentle-force/recaptcha/unlock',
$response->getContent(),
'Expected response to contain unlock URL'
);
$this->assertStringContainsString(
$this->checkIfStringExists(
'Custom template',
$response->getContent(),
'Expected response to be generated from customized template'
);
}

private function checkIfStringExists(string $needle, string $haystack, string $message)
{
if (mb_strpos($haystack, $needle) === false) {
$this->fail($message);
}
}
}

0 comments on commit 801b950

Please sign in to comment.