Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fatal error: Uncaught exception 'Predis\Connection\ConnectionException' with message 'Connection refused [tcp://127.0.0.1:6379]' #223

Closed
tillkruss opened this issue Nov 18, 2014 · 8 comments

Comments

@tillkruss
Copy link
Member

I'm trying to catch all Predis exceptions, but even if I use catch ( \Exception $exception ) I still get a fatal error. How can I catch all exceptions? This is my code:

<?php

function cache_init() {
    new SomeClass();
}

class SomeClass {

    public function __construct() {

        try {

            require_once 'predis.php';
            Predis\Autoloader::register();
            $this->redis = new Predis\Client();

        } catch ( Exception $exception ) {
            ...
        }

    }

}
@nrk
Copy link
Contributor

nrk commented Nov 18, 2014

Connections are lazy which means that Predis doesn't open the connection when initializing the client instance but when sending the first command to Redis. If you need to catch the exception when trying to connect, you should explicitly call Predis\Client::connect() wrapped in a try ... catch ... block:

$client = new Predis\Client();

try {
    $client->connect();
} catch (Predis\Connection\ConnectionException $exception) {
    // ...
}

I don't know why you need to catch all the exceptions but Predis\Connection\ConnectionException is all you need in that context.

@nrk nrk closed this as completed Nov 18, 2014
@nrk nrk added the invalid label Nov 18, 2014
@tillkruss
Copy link
Member Author

Thanks a lot, that's what I needed to know.

@ivtod
Copy link

ivtod commented Mar 2, 2018

Hi @nrk,

as you stated in the doc about connecting to redis (https://github.com/nrk/predis#connecting-to-redis) when you create a redis client with $client = new Predis\Client(); it will automatically connect using default params.

When creating a client instance without passing any connection parameter, Predis assumes 127.0.0.1 and 6379 as default host and port. The default timeout for the connect() operation is 5 seconds

So how can I catch a connection exception?
The above solution you provided unfortunately doesn't work for me. Am I missing something?

Thanks!

P.S.
How can I change the default timeout for the connect() operation?
I don't need to heavily rely on redis to connect, so what I need to do is try to use it if everything is ok, but if something doesn't work or the connection is slow I need to move on.

@nrk
Copy link
Contributor

nrk commented Mar 2, 2018

@ivtod I'm not sure if you are missing something, but I'm definitely missing any detail on how exactly it's not working for you: it doesn't throw any exception? it throws a different exception? it fails later in the script? Also, which version of PHP and Predis are you using? I can't help you if you don't disclose a bit more details...

As for specifying a timeout for the connect() operation, simply provide an explicit URI to the client constructor with a timeout connection parameter expressed in seconds, e.g. tcp://127.0.0.1:6379?timeout=0.050.

@ivtod
Copy link

ivtod commented Mar 2, 2018

I'm sorry @nrk, I was able to make it work.
The try{}catch(){} on the connect() method call was ok, I've found out that the fatal error was caused by a successive call to the hget() method throwing Connection refused [tcp://127.0.0.1:6379], and I thought it was an error due to the connect() method that seems to have the same error message.

Thank you very much for your help

@cendekia
Copy link

I'm sorry @nrk, I was able to make it work.
The try{}catch(){} on the connect() method call was ok, I've found out that the fatal error was caused by a successive call to the hget() method throwing Connection refused [tcp://127.0.0.1:6379], and I thought it was an error due to the connect() method that seems to have the same error message.

Thank you very much for your help

@ivtod
Could you give me the details of how you solved this? It's happening to me now, please help :)

@ivtod
Copy link

ivtod commented Oct 13, 2018

Hi @cendekia,
Would you provide more details about your code?
As mentioned in the previous comments, connection is lazy, so the fatal error is not thrown on the connect() method invocation, but, as in my case, by a successive call to the hget() method throwing Connection refused.
I've solved this by creating an "absorber" class initialized in the catch(){} that simply tells me what method is invoked and then I can decide how to handle it.
I'm sure that @nrk can tell us a better and more elegant way to handle it :)

@cendekia
Copy link

Hi @ivtod,
I did make it work too.
In my case, I'm using laravel and I was tried to make a test case when the redis is not active it will switched to use memchaced instead, then I realized that laravel by default is using cache on their default installation, so I did fix it by putting try{} catch(){} on the service provider before it touch my route controller. I think it's pretty similar to your solution.

Btw, thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants