diff --git a/CHANGELOG.md b/CHANGELOG.md index e36b147..e052485 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Missing default values on highlight option ([Issue#38]) - Don't throw an exception if the result of the index definition is incorrect ([Issue#46]) - Phpredis extension don't return raw response ([Issue#46]) +- Error with version reading if `MODULE LIST` command is protected ([PR#42]) ### Changed @@ -247,3 +248,4 @@ First version [PR#14]: https://github.com/MacFJA/php-redisearch/pull/14 [PR#15]: https://github.com/MacFJA/php-redisearch/pull/15 [PR#17]: https://github.com/MacFJA/php-redisearch/pull/17 +[PR#42]: https://github.com/MacFJA/php-redisearch/pull/42 diff --git a/src/Redis/Initializer.php b/src/Redis/Initializer.php index 865f3af..8c72dbf 100644 --- a/src/Redis/Initializer.php +++ b/src/Redis/Initializer.php @@ -52,6 +52,7 @@ use MacFJA\RediSearch\Redis\Command\TagVals; use Predis\Profile\RedisProfile; use Rediska_Commands; +use Throwable; /** * @SuppressWarnings(PHPMD.CouplingBetweenObjects) @@ -139,7 +140,15 @@ public static function registerCommandsRediska(): void public static function getRediSearchVersion(Client $client): ?string { - $modules = $client->executeRaw('module', 'list') ?? []; + try { + $modules = $client->executeRaw('module', 'list') ?? []; + } catch (Throwable $exception) { + if (false === stripos($exception->getMessage(), 'unknown command')) { + throw $exception; + } + + return null; + } foreach ($modules as $module) { $data = array_column( diff --git a/tests/fixes/PR42Test.php b/tests/fixes/PR42Test.php new file mode 100644 index 0000000..07063cf --- /dev/null +++ b/tests/fixes/PR42Test.php @@ -0,0 +1,55 @@ +createMock(Client::class); + $client->method('executeRaw')->willThrowException(new Exception('ERR unknown command `module`, with args beginning with: `list`')); + + $response = Initializer::getRediSearchVersion($client); + + static::assertNull($response); + } + + public function testUnknownError(): void + { + $this->expectException(Exception::class); + + $client = $this->createMock(Client::class); + $client->method('executeRaw')->willThrowException(new Exception('ERR unknown server error')); + + Initializer::getRediSearchVersion($client); + } +}