gpt4 book ai didi

php - Predis - 捕获连接错误

转载 作者:可可西里 更新时间:2023-11-01 00:43:06 28 4
gpt4 key购买 nike

您好,我有以下内容:

// 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 之类的错误。

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

最佳答案

我广泛阅读了有关此问题的内容,尽管我的配置有所不同,因为它使用了多个服务器,而且我试图解决的问题是服务器出现故障,但这似乎已为我解决了:

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

我尝试了很多变化,这似乎阻止了应用程序的所有尖叫。在我的例子中,我传入了一组服务器并尝试对它们执行 ping 操作并删除失败的服务器。这似乎有点矫枉过正,但如果不这样做,我怎么知道服务器是否已关闭?

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

关于php - Predis - 捕获连接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28394489/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com