【发布时间】:2019-03-30 05:34:57
【问题描述】:
我需要连接一个 API,所以我写了一个函数:
try {
$res4 = $client3->post('https://api.example.co.uk/Book', [
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ajhsdbjhasdbasdbasd',
],
'json' => [
'custFirstName' => $FirstName,
'custLastName' => $Surname,
'custPhone' => $Mobile,
'custEmail' => $Email,
]
]);
} catch (GuzzleHttp\Exception\ClientException $e) {
$response = $e->getResponse();
$result = json_decode($response->getBody()->getContents());
$item->update(['status' => 'Problems at step3']);
Mail::raw('Problem at STEP 3', function ($message) use ($serial) {
$message->from('asd.asd@gmail.com', 'asd.asd@gmail.com');
$message->subject('We got a problem etc.');
$message->to('john.smith@gmail.com');
});
}
如您所见,我需要调用 API,但在 API 关闭的情况下,我会编写 catch 函数。
但是现在当 API 关闭并且 API 返回“500 内部错误”时,这个函数就崩溃了......
我的问题是为什么 catch 不处理它?
我如何处理错误 - 当 API 关闭或请求错误时...为什么 catch{} 不起作用?
更新:这是我的 laravel.log
[2018-10-25 14:51:04] local.ERROR: GuzzleHttp\Exception\ServerException: Server error: `POST https://api.example.co.uk/Book` resulted in a `500 Internal Server Error` response:
{"message":"An error has occured. Please contact support."}
in /home/public_html/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:107
Stack trace:
#0 /home/public_html/vendor/guzzlehttp/guzzle/src/Middleware.php(65): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Psr7\Request), Object(GuzzleHttp\Psr7\Response))
#1 /home/public_html/vendor/guzzlehttp/promises/src/Promise.php(203): GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Response))
【问题讨论】:
-
请查看文件夹/storage/log/laravel.log中的laravel日志
-
它可能不是
ClientException而是捕获根异常Exception -
检查您的日志并在此处发布完整的错误。
-
500内部错误不是顺便抛出的异常。
-
从 API 我收到错误:RequestException.php 第 107 行中的 ServerException:服务器错误:
POST https://api.exampe.co.uk/Book导致500 Internal Server Error响应:{“消息”:“发生错误。请联系支持."}
标签: php laravel error-handling guzzle guzzle6