我真的不明白如何在onReject处理程序中捕获异常(转发它)。我想知道是否有人能指出我如何成功地做到这一点的正确方向。
我发送了一些异步请求,当一个失败时遇到“遇到未捕获的异常 - 类型:GuzzleHttp \ Exception \ ClientException”它永远不会被捕获。
我读过了:
但目前尚不清楚为什么以下不起作用。我的理解是当在onReject(RequestException)中抛出ClientException时,它会将其进一步推送到下一个onReject(ClientException)并被正确捕获。
任何帮助,将不胜感激。
$client = new GuzzleHttp\Client();
$promise = $client->requestAsync('POST', SOME_URL, [
... SOME_PARAMS ...
]);
$promise->then(
function (ResponseInterface $res) {
//ok
},
function (RequestException $e) {
//inside here throws a ClientException
}
)->then(null, function (ClientException $e) {
//Why does it not get caught/forwarded to this error handler?
});
根据guzzle文档,
如果在$ onRejected回调中抛出异常,则会以抛出的异常为原因调用后续的$ onRejected回调。
所以这应该工作:
$promise
->then(
function (ResponseInterface $res) {
// this will be called when the promise resolves
return $someVal;
},
function (RequestException $e) {
// this will be called when the promise resolving failed
// if you want this to bubble down further the then-line, just re-throw:
throw $e;
}
)
->then(
function ($someVal) {
},
function (RequestException $e) {
// now the above thrown Exception should be passed in here
});
Guzzle承诺follow承诺/ A +标准。因此,我们可以依靠official description来掌握您对此感到好奇的行为:
2.2.7.1.如果onFulfilled或onRejected返回值
x
,请运行Promise Resolution Procedure[[Resolve]](promise2, x)
。2.2.7.2.如果
onFulfilled
或onRejected
抛出一个例外e
,promise2
必须以e
为理由拒绝。
后来针对2.2.7.2案例:
2.3.2.如果
x
是一个承诺,采取其状态
因此,您可以按照@lkoell提出的解决方案或从回调中返回RejectedPromise
,这将强制后续承诺采用rejected
状态。
$promiseA = $promise
->then(
function (ResponseInterface $res) {
//ok
},
function (RequestException $e) {
//This will force $promiseB adopt $promiseC state and get rejected
return $promiseC = new RejectedPromise($clientException);
}
);
$promiseB = $promiseA->then(null, function (ClientException $e) {
// There you will get rejection
});
这种方式更灵活,因为您不仅可以拒绝承诺,而且可以拒绝任何原因(承诺除外)。