如果 Guzzle 的答案是 404,如何避免抛出异常?
How to avoide throwing exception if an answer is 404 with Guzzle?
在我的情况下,不是结果也是结果。但是 Guzzle
抛出异常,结果我的承诺状态为 'rejected'。是否有可能改变这种行为?
如果不可能,我可以获取状态码吗?如果我这样写:
$item['reason']->code
我有一个错误。我认为这个领域是私人的。而且这个对象没有getStatusCode
方法。
所以我需要 200 和 404 个结果。 500等等对我来说不重要
通常我们只是捕获这些异常并提供一些逻辑,例如:
$emptyResult = [];
try{
//your code that should work goes here
$result = getItemsFromApi();
return json_encode($result);
} catch(ClientException $e){
if($e->getCode() == '404'){
/*warning: in our case body is json already, not sure if your is the same */
return $e->getResponse()->getBody();
}
}
在我的情况下,不是结果也是结果。但是 Guzzle
抛出异常,结果我的承诺状态为 'rejected'。是否有可能改变这种行为?
如果不可能,我可以获取状态码吗?如果我这样写:
$item['reason']->code
我有一个错误。我认为这个领域是私人的。而且这个对象没有getStatusCode
方法。
所以我需要 200 和 404 个结果。 500等等对我来说不重要
通常我们只是捕获这些异常并提供一些逻辑,例如:
$emptyResult = [];
try{
//your code that should work goes here
$result = getItemsFromApi();
return json_encode($result);
} catch(ClientException $e){
if($e->getCode() == '404'){
/*warning: in our case body is json already, not sure if your is the same */
return $e->getResponse()->getBody();
}
}