redis 错误处理

redis组件根据错误的级别,区分了2种错误信息

异常

当redis连接失败,无法和redis服务通信时,将会抛出EasySwoole\Redis\Exception\RedisException 异常,例如配置错误:

  1. PHP Fatal error: Uncaught EasySwoole\Redis\Exception\RedisException: connect to redis host 127.0.0.1:6379 fail after retry 4 times in /www/easyswoole/tioncico_redis/src/Redis.php:2866
  2. Stack trace:
  3. #0 /www/easyswoole/tioncico_redis/src/Redis.php(579): EasySwoole\Redis\Redis->sendCommand(Array)
  4. #1 /www/easyswoole/tioncico_redis/tests/test.php(17): EasySwoole\Redis\Redis->get('a')
  5. #2 {main}
  6. thrown in /www/easyswoole/tioncico_redis/src/Redis.php on line 2866

我们只需要接管该异常即可:

  1. go(function () {
  2. $redisConfig = new \EasySwoole\Redis\Config\RedisConfig();
  3. $redisConfig->setAuth('easyswoole');
  4. $redis = new \EasySwoole\Redis\Redis($redisConfig);
  5. try{
  6. $data = $redis->rawCommand(['set','a','1','1']);//多了一个参数,redis将会报语法错误
  7. var_dump($data);
  8. }catch (\EasySwoole\Redis\Exception\RedisException $exception){
  9. var_dump($exception->getMessage());
  10. var_dump($exception->getRedisErrorCode());
  11. var_dump($exception->getRedisErrorMsg());
  12. }
  13. });