【问题标题】:Predis - Catch connection errorPredis - 捕获连接错误
【发布时间】:2015-02-08 13:32:17
【问题描述】:

您好,我有以下内容:

// Named array of connection parameters:
    $redis = new Predis\Client([
        'scheme' => 'tcp',
        'host'   => $host,
        'port'   => $port,
        'password'   => $auth,
    ]);

    try {
        // explicitly call Predis\Client::connect()
        $redis->connect();
    }
    catch (Exception $e) {
        return Redirect::to('profile')
                ->with('result', '<div class="col-lg-12"><div class="alert alert-danger alert-border-left alert-gradient alert-dismissable">
                <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                <i class="fa fa-check pr10"></i>
                <strong>Error - '.$e.'</strong> Unable to connect to redis server, please double check your database detals.</div></div>');
    }

但是这并没有捕获诸如 NOAUTH 之类的错误。

有人能指出正确的方向吗?

【问题讨论】:

  • 试试catch (\Exception $e)
  • 嗨,不,仍然没有发现错误

标签: php laravel predis


【解决方案1】:

我广泛阅读了有关此问题的信息,虽然我的配置不同,因为它使用多个服务器,而且我试图解决的问题是服务器故障,但这似乎已经为我解决了:

 try {
      $client = new Predis\Client($params);
      $client->connect();
 } catch(Predis\Connection\ConnectionException $e) {
      echo $e->getMessage();
 }

我尝试了许多变化,这似乎停止了应用程序的所有尖叫。在我的情况下,我传入了一组服务器并尝试 ping 它们并删除失败的服务器。这似乎有点矫枉过正,但如果不这样做,我怎么知道服务器是否关闭?

另外,如果你不尝试连接,那么它会在其他地方失败,也就是说,连接是惰性的,并且会在实际需要时调用——对我来说,这总是导致连接错误。在使用它之前自己调用它可以让我捕捉到异常。然后我必须找出异常为什么不做,不得不将它切换到 Predis\Connection\ConnectionException。这比原本应该的要困难得多。

【讨论】:

    猜你喜欢
    • 2015-12-02
    • 2011-09-14
    • 1970-01-01
    • 2016-01-06
    • 2016-01-29
    • 1970-01-01
    • 1970-01-01
    • 2015-12-05
    • 1970-01-01
    相关资源
    最近更新 更多