改进 异常抛出

This commit is contained in:
iVampireSP.com 2023-02-05 20:44:42 +08:00
parent 9fa859211e
commit 94d7e13c6a
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
2 changed files with 20 additions and 7 deletions

View File

@ -26,12 +26,7 @@ public function call(Request $request, Module $module): Response|JsonResponse
$method = Str::lower($request->method());
try {
$response = $module->request($method, $path, $request->all());
} catch (ConnectException $e) {
Log::error('在执行 call ' . $method . ' ' . $path . ' 时发生错误: ' . $e->getMessage());
return $this->serviceUnavailable();
}
$response = $module->request($method, $path, $request->all());
if ($response['json'] === null && $response['body'] !== null) {
return response($response['body'], $response['status']);

View File

@ -8,6 +8,7 @@
use GuzzleHttp\Exception\ConnectException;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Cache;
@ -113,9 +114,26 @@ private function getResponse(Response $response): array
];
}
/**
* @param $method
* @param $path
* @param $requests
*
* @return array
*/
public function request($method, $path, $requests): array
{
return $this->baseRequest($method, "functions/$path", $requests);
try {
return $this->baseRequest($method, "functions/$path", $requests);
} catch (ConnectException|ConnectionException $e) {
Log::error('在执行 call ' . $method . ' ' . $path . ' 时发生错误: ' . $e->getMessage());
return [
'body' => null,
'json' => null,
'status' => 502,
'success' => false,
];
}
}
public function baseRequest($method, $path, $requests = []): array