Handled exceptions that are still exiting stack

I posted this in extensions but decided to repost here since it really is an exception issue.

What do you do when the redis connection is down? I’ve been pulling my hair out on this. Everything works great when redis is up and running. So I wanted to test and build a solid fall back to the DB when redis was down. The problem became apparent when I realized that the init() of CRedisCache does not create a connection or ping for the sign of a connection. But once I execute a command against the cache, if the connection is down Predis will throw the exception Predis_CommunicationException.

I never thought this would be an issue. Anytime I called a Yii::app()->cache-> command I wrapped it in try/catch blocks and caught Predis_CommunicationException. My problem is that even if I catch the error, it still exists in the Yii exception head space and it still kills the stack at a later point.

When I put the following in my config/main.php, it eliminates the display of any error data which was an earlier problem. Unfortunately, the exception is still sitting in the errorHandler on CApplication(i think that’s the obj location). And because of that, CApplication->handleError is still called down the stack and it exits the app instead of skipping over the exception.




'onException' => function($event)

{

    if ($event->exception instanceof Predis_CommunicationException)

    {

        $event->handled = true;

    }

},



When I attempt to get a key from redis, I call this. Be reminded, when redis is up this works like a charm. It’s only the persistence of the exception (when redis is down) in Yii’s errorHandler that is the problem site wide.




try

{

    $debtorCache = Yii::app()->cache->get($key);

    if(empty($debtorCache))

    {

        // this works great....

        // get data from db..

	// set cache data

	// return data

    }

    else

    {

        // this works great....

        list($debtorData, $caseData, $paymentData, $activityData) = $debtorCache;

    }

}

catch(Predis_CommunicationException $e)

{

    // displays just fine before CApplication handleError exits....

    echo "{$e->getFile()}:{$e->getLine()}: Redis is down..<br>";

}



I kept getting a standard Yii uncaught exception that gave the error name and message which killed the app.

FYI: I use PHPStorm with xdebug. I ran through the stack on this one line by line, and stepped into every call. I couldn’t figure out why the Predis exception was still being called after I caught it. Looks like even setting an event->exception to “handled” still gets a exit(1) from CApplication->handleError. Curious why.

I’m starting to think this has something to do with how Yii manages “uncaught” exceptions in the handleError() in CApplication. Not sure what to make of this since I have caught the error everytime I call Yii::app()->cache, and by using CApplication->onException in config/main.php, setting $event->handled = true.

Any help would be greatly appreciated.