为什么在promise中Guzzle onrejected函数——然后仍然抛出异常?

问题描述 投票:0回答:1

我发现$promise->wait()函数会抛出异常,我必须再次使用try-catch来处理异常。

$promise = $this->client->sendAsync($request);
$promise->then(
    function ($response) use (...){
        //do something
    },
    function ($exception) {
        $this->logger->error("Failed " . $exception->getMessage());
    }
);
try{
    $promise->wait(true);
}catch(Exception $exception){
    $this->logger->error("Failed " . $exception->getMessage());
}

错误信息将被记录器记录两次。 onrejected函数中有没有可以处理异常的方法?或者有更好的方法来处理此类异常吗?

这是我的错误: PHP 致命错误:未捕获的 GuzzleHttp\Exception\ClientException:客户端错误:...

非常感谢您的帮助。

这个我试过了

$promise->then(
//...
)->then(
//...
);

但不工作。

php guzzle guzzle6
1个回答
0
投票

好吧,也许我明白了。 更换

try{
    $promise->wait(true);
}catch(Exception $exception){
    $this->logger->error("Failed " . $exception->getMessage());
}

Promise\Utils::settle($promise)->wait();

$promise->wait(false);
© www.soinside.com 2019 - 2024. All rights reserved.