【发布时间】:2017-11-01 14:14:17
【问题描述】:
我正在通过 Guzzle 发送帖子,并且某些产品返回 InvalidArgumentException 错误,我如何获取此数据?不幸的是,文档中对此一无所知。
这是错误
InvalidArgumentException in functions.php line 324:
json_encode error: Malformed UTF-8 characters, possibly incorrectly encoded
我的收获:
catch (ClientException $e) {
if ($e->hasResponse()) {
$resposta = Psr7\str($e->getResponse());
$incrementer->cron_value = $incrementer->cron_value + 1;
$incrementer->save();
return view('home');
}
}
catch (RequestException $e) {
return view('home');
if ($e->hasResponse()) {
$incrementer->cron_value = $incrementer->cron_value + 1;
$incrementer->save();
return view('home');
}
}
【问题讨论】:
-
始终在 try ..catch 块中发送您的请求,例如
try { // Your guzzle action} catch (Exception $e) {echo $e->getMessage(); } -
是的,我使用了 try catch,但在 Guzzle 文档中我没有看到任何方法可以得到这种错误。比如我使用了ClientException和RequestException,但是由于错误是InvalidArgumentException,所以没有通过try catch过滤
-
如果操作正确,所有异常都可以被捕获。向我们展示您的
try/catch。 -
好的,我编辑了。 @ceejayoz
-
只需添加另一个
catch...例如catch(InvalidArgumentException $e)。 1 个try可以有多个catch()块。或者,您可以通过执行(catch \Exception $e)捕获任何异常,然后将$e传递给另一个可以switch处理异常类型的类,就像 Larvael(在这种情况下是 Symfony)所做的那样。