Skip to content

Commit 99edf20

Browse files
committed
Throw exception when RediSearch module isn't loaded
1 parent 9d06bca commit 99edf20

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Ehann\RediSearch\Exceptions;
4+
5+
use Exception;
6+
7+
class UnknownRediSearchCommandException extends Exception
8+
{
9+
public function __construct($message = '', $code = 0, Exception $previous = null)
10+
{
11+
parent::__construct(
12+
trim("Unknown RediSearch command. Are you sure the RediSearch module is enabled in Redis? $message"),
13+
$code,
14+
$previous
15+
);
16+
}
17+
}

src/Redis/RedisClient.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Ehann\RediSearch\Exceptions\InvalidRedisClientClassException;
66
use Ehann\RediSearch\Exceptions\UnknownIndexNameException;
7+
use Ehann\RediSearch\Exceptions\UnknownRediSearchCommandException;
78
use Ehann\RediSearch\Exceptions\UnsupportedLanguageException;
89
use Psr\Log\LoggerInterface;
910

@@ -72,16 +73,24 @@ public function rawCommand(string $command, array $arguments)
7273
$rawResult = $this->isPredisClient() ?
7374
$this->redis->executeRaw($arguments) :
7475
call_user_func_array([$this->redis, 'rawCommand'], $arguments);
76+
$this->throwExceptionIfRawResultIndicatesAnError($rawResult);
77+
return $rawResult;
78+
}
7579

80+
public function throwExceptionIfRawResultIndicatesAnError($rawResult)
81+
{
82+
if (!is_string($rawResult)) {
83+
return;
84+
}
7685
if ($rawResult === 'Unknown Index name') {
7786
throw new UnknownIndexNameException();
7887
}
79-
$unsupportedLanguageMessages = ['Unsupported Language', 'Unsupported Stemmer Language'];
80-
if (in_array($rawResult, $unsupportedLanguageMessages)) {
88+
if (in_array($rawResult, ['Unsupported Language', 'Unsupported Stemmer Language'])) {
8189
throw new UnsupportedLanguageException();
8290
}
83-
84-
return $rawResult;
91+
if (strpos($rawResult, 'ERR unknown command \'FT.') !== false) {
92+
throw new UnknownRediSearchCommandException($rawResult);
93+
}
8594
}
8695

8796
public function setLogger(LoggerInterface $logger): RedisClient

0 commit comments

Comments
 (0)