从 guzzle 捕获异常
Catch exception from guzzle
我正在使用 laravel 并且我已经设置了抽象 class 方法来从我调用的各种 API 中获取响应。但是,如果 API url 无法访问,则会抛出异常。我知道我错过了什么。任何帮助对我来说都会很棒。
$offers = [];
try {
$appUrl = parse_url($this->apiUrl);
// Call Api using Guzzle
$client = new Client('' . $appUrl['scheme'] . '://' . $appUrl['host'] . '' . $appUrl['path']);
if ($appUrl['scheme'] == 'https') //If https then disable ssl certificate
$client->setDefaultOption('verify', false);
$request = $client->get('?' . $appUrl['query']);
$response = $request->send();
if ($response->getStatusCode() == 200) {
$offers = json_decode($response->getBody(), true);
}
} catch (ClientErrorResponseException $e) {
Log::info("Client error :" . $e->getResponse()->getBody(true));
} catch (ServerErrorResponseException $e) {
Log::info("Server error" . $e->getResponse()->getBody(true));
} catch (BadResponseException $e) {
Log::info("BadResponse error" . $e->getResponse()->getBody(true));
} catch (\Exception $e) {
Log::info("Err" . $e->getMessage());
}
return $offers;
不确定您如何声明这些异常以及您使用的是哪个 Guzzle 版本,但在 official documentation 中不存在这些异常。
在您的情况下,您可能缺少 GuzzleHttp\Exception\ConnectException
。
guzzle 官方没有定义这些异常。
这些异常在 AWS SDK for PHP 中定义。
对于官方Guzzle你可以按照下面的方式做。
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception\ClientException;
....
try {
$response = $this->client->request($method, $url, [
'headers' => $headers,
'form_params' => $form_parameters,
]);
$body = (string)$response->getBody();
} catch (ClientException $e) {
// Do some thing here...
} catch (RequestException $e) {
// Do some thing here...
} catch (\Exception $e) {
// Do some thing here...
}
你应该使用 'http_errors' => false,
选项设置 guzzehttp 客户端
示例代码应该是这样的,document:guzzlehttp client http-error option explain
Set to false to disable throwing exceptions on an HTTP protocol errors (i.e., 4xx and 5xx responses). Exceptions are thrown by default when HTTP protocol errors are encountered.
$client->request('GET', '/status/500');
// Throws a GuzzleHttp\Exception\ServerException
$res = $client->request('GET', '/status/500', ['http_errors' => false]);
echo $res->getStatusCode();
// 500
$this->client = new Client([
'cookies' => true,
'headers' => $header_params,
'base_uri' => $this->base_url,
'http_errors' => false
]);
$response = $this->client->request('GET', '/');
if ($code = $response->getStatusCode() == 200) {
try {
// do danger dom things in here
}catch (/Exception $e){
//catch
}
}
您可以创建自己的例外
class CustomException 扩展异常
{
}
接下来你从抽象 class guzzle 异常中抛出你自己的异常
catch(ConnectException $ConnectException)
{
throw new CustomException("Api excception ConnectException",1001);
}
然后在全局异常处理程序渲染方法中处理
if($exception instanceof CustomException)
{
dd($exception);
}
我正在使用 laravel 并且我已经设置了抽象 class 方法来从我调用的各种 API 中获取响应。但是,如果 API url 无法访问,则会抛出异常。我知道我错过了什么。任何帮助对我来说都会很棒。
$offers = [];
try {
$appUrl = parse_url($this->apiUrl);
// Call Api using Guzzle
$client = new Client('' . $appUrl['scheme'] . '://' . $appUrl['host'] . '' . $appUrl['path']);
if ($appUrl['scheme'] == 'https') //If https then disable ssl certificate
$client->setDefaultOption('verify', false);
$request = $client->get('?' . $appUrl['query']);
$response = $request->send();
if ($response->getStatusCode() == 200) {
$offers = json_decode($response->getBody(), true);
}
} catch (ClientErrorResponseException $e) {
Log::info("Client error :" . $e->getResponse()->getBody(true));
} catch (ServerErrorResponseException $e) {
Log::info("Server error" . $e->getResponse()->getBody(true));
} catch (BadResponseException $e) {
Log::info("BadResponse error" . $e->getResponse()->getBody(true));
} catch (\Exception $e) {
Log::info("Err" . $e->getMessage());
}
return $offers;
不确定您如何声明这些异常以及您使用的是哪个 Guzzle 版本,但在 official documentation 中不存在这些异常。
在您的情况下,您可能缺少 GuzzleHttp\Exception\ConnectException
。
guzzle 官方没有定义这些异常。
这些异常在 AWS SDK for PHP 中定义。
对于官方Guzzle你可以按照下面的方式做。
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception\ClientException;
....
try {
$response = $this->client->request($method, $url, [
'headers' => $headers,
'form_params' => $form_parameters,
]);
$body = (string)$response->getBody();
} catch (ClientException $e) {
// Do some thing here...
} catch (RequestException $e) {
// Do some thing here...
} catch (\Exception $e) {
// Do some thing here...
}
你应该使用 'http_errors' => false,
选项设置 guzzehttp 客户端
示例代码应该是这样的,document:guzzlehttp client http-error option explain
Set to false to disable throwing exceptions on an HTTP protocol errors (i.e., 4xx and 5xx responses). Exceptions are thrown by default when HTTP protocol errors are encountered.
$client->request('GET', '/status/500');
// Throws a GuzzleHttp\Exception\ServerException
$res = $client->request('GET', '/status/500', ['http_errors' => false]);
echo $res->getStatusCode();
// 500
$this->client = new Client([
'cookies' => true,
'headers' => $header_params,
'base_uri' => $this->base_url,
'http_errors' => false
]);
$response = $this->client->request('GET', '/');
if ($code = $response->getStatusCode() == 200) {
try {
// do danger dom things in here
}catch (/Exception $e){
//catch
}
}
您可以创建自己的例外
class CustomException 扩展异常 { }
接下来你从抽象 class guzzle 异常中抛出你自己的异常
catch(ConnectException $ConnectException)
{
throw new CustomException("Api excception ConnectException",1001);
}
然后在全局异常处理程序渲染方法中处理
if($exception instanceof CustomException)
{
dd($exception);
}