refactor: 规范状态码、抛出异常的使用

This commit is contained in:
xboard
2023-12-07 04:01:32 +08:00
parent c25803aa74
commit 189b247ad8
71 changed files with 663 additions and 910 deletions

View File

@@ -22,10 +22,10 @@ class TrojanTidalabController extends Controller
{
$token = $request->input('token');
if (empty($token)) {
throw new ApiException(500, 'token is null');
throw new ApiException('token is null');
}
if ($token !== admin_setting('server_token')) {
throw new ApiException(500, 'token is error');
throw new ApiException('token is error');
}
}
@@ -36,7 +36,7 @@ class TrojanTidalabController extends Controller
$nodeId = $request->input('node_id');
$server = ServerTrojan::find($nodeId);
if (!$server) {
throw new ApiException(500, 'fail');
return $this->fail([400,'节点不存在']);
}
Cache::put(CacheKey::get('SERVER_TROJAN_LAST_CHECK_AT', $server->id), time(), 3600);
$serverService = new ServerService();
@@ -89,15 +89,18 @@ class TrojanTidalabController extends Controller
// 后端获取配置
public function config(Request $request)
{
$nodeId = $request->input('node_id');
$localPort = $request->input('local_port');
if (empty($nodeId) || empty($localPort)) {
throw new ApiException(422, '参数错误');
}
$request->validate([
'node_id' => 'required',
'local_port' => 'required'
],[
'node_id.required' => '节点ID不能为空',
'local_port.required' => '本地端口不能为空'
]);
try {
$json = $this->getTrojanConfig($nodeId, $localPort);
$json = $this->getTrojanConfig($request->input('node_id'), $request->input('local_port'));
} catch (\Exception $e) {
throw new ApiException(500, $e->getMessage());
\Log::error($e);
return $this->fail([500,'配置获取失败']);
}
return(json_encode($json, JSON_UNESCAPED_UNICODE));
@@ -107,7 +110,7 @@ class TrojanTidalabController extends Controller
{
$server = ServerTrojan::find($nodeId);
if (!$server) {
throw new ApiException(500, '节点不存在');
return $this->fail([400, '节点不存在']);
}
$json = json_decode(self::TROJAN_CONFIG);