完善请求处理公共类的异常处理

This commit is contained in:
fantasticbin 2022-11-14 14:17:01 +08:00
parent 1ff1dceab7
commit 144caadd3f

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Helper;
use App\Exception\BusinessException;
use GuzzleHttp\Client;
use Hyperf\Guzzle\HandlerStackFactory;
@ -35,7 +36,6 @@ class Curl
* @param string $url
* @param array $headers
* @return array
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public static function get(string $url, array $headers = []) : array
{
@ -46,7 +46,11 @@ class Curl
$options['headers'] = $headers;
}
$response = $client->get($url, $options);
try {
$response = $client->get($url, $options);
} catch (\Throwable $throwable) {
throw new BusinessException($throwable->getCode(), $throwable->getMessage());
}
if ($response->getStatusCode() === 200) {
return json_decode($response->getBody()->getContents(), true);
@ -62,7 +66,6 @@ class Curl
* @param array $data
* @param array $headers
* @return array
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public static function post(string $url, array $data = [], array $headers = []) : array
{
@ -73,7 +76,11 @@ class Curl
$options['headers'] = $headers;
}
$response = $client->post($url, $options);
try {
$response = $client->post($url, $options);
} catch (\Throwable $throwable) {
throw new BusinessException($throwable->getCode(), $throwable->getMessage());
}
if ($response->getStatusCode() === 200) {
return json_decode($response->getBody()->getContents(), true);