记录使用 guzzlehttp 异常捕获踩坑记录
记录一下自己项目中的坑, 如有不对之处希望大家指正。
在之前公司项目工作中,经常对接海外的支付,常常用到guzzlehttp 用作接口请求的依赖包,由于没有查阅文档和代码,捕获异常处理的不正确, 导致重复支付的现象。
try {
$result = $client->request('post', $url, $options);
} catch (\Throwable $throwable) {
//donging something
return false;
}
最开始的时候简单的这样捕获异常,以为这样的异常都是请求失败,但是后面由于重复支付的问题,查看了guzzlehttp的代码,发现他们设置了处理请求的中间件,代码如下:
public static function httpErrors()
{
return function (callable $handler) {
return function ($request, array $options) use ($handler) {
if (empty($options['http_errors'])) {
return $handler($request, $options);
}
return $handler($request, $options)->then(
function (ResponseInterface $response) use ($request, $handler) {
$code = $response->getStatusCode();
if ($code < 400) {
return $response;
}
throw RequestException::create($request, $response);
});
};
};
}
由于上面的可以看出,guzzlehttp 根据请求的httpcode 做了不同的响应,只正常返回<=400以下的响应,其余的都抛出的异常,主要是俩类异常4XX 和5XX的异常类,代码如下:
$level = (int) floor($response->getStatusCode() / 100);
if ($level === 4) {
$label = 'Client error';
$className = ClientException::class;
} elseif ($level === 5) {
$label = 'Server error';
$className = ServerException::class;
} else {
$label = 'Unsuccessful request';
$className = __CLASS__;
}
所以最后我在请求异常处理里面根据不同的异常类分别处理, 这样就可以分别判断请求状态,哪些可以重试,哪些支付不能重试。避免了类似5XX 的服务端的错误,在项目中进行了支付重试,修改后代码如下:
try {
$result = $client->request('post', $url, $options);
} catch (\Throwable $throwable) {
if ($throwable instanceof ClientException) {
//doing something
return ...
}
if ($throwable instanceof ServerException) {
//doing something
return ...
}
//doing something
return false;
}
以上是我记录guzzlehttp 的异常捕获遇到的坑,如有错误请大家指正
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: