Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Predis - Catch connection error

Hi I have the following:

// 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>');
    }

However this isn't catching errors such as NOAUTH.

Can someone point me in the correct direction?

like image 844
Ricky Barnett Avatar asked Oct 29 '25 05:10

Ricky Barnett


1 Answers

I read extensively about this issue, and although my configuration is different since it is using multiple servers, and the problem I was trying to solve was a failed server, this seems to have done it for me:

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

I tried many variations and this seems to stop all the screaming from the application. In my case I passed in an array of servers and attempt to ping them and remove the server which fails. It seems like overkill but how else will I know if the server is down without doing that?

Also, if you do not attempt a connection then it will fail somewhere else, that is, the connection is lazy and will be called when it is actually needed--for me this always resulted in a connection error. Calling it myself before using it allowed me to catch the exception. Then I had to find out why Exception would not do and had to switch it to Predis\Connection\ConnectionException. It was way harder than it should have been.

like image 130
Andrew Cotton Avatar answered Oct 31 '25 20:10

Andrew Cotton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!