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

develop
fantasticbin 2 years ago
parent 1ff1dceab7
commit 144caadd3f

@ -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);

Loading…
Cancel
Save