refactor: 规范Expection处理

This commit is contained in:
xboard
2023-12-04 20:40:49 +08:00
parent aa0fe64afe
commit 0ab7dee52d
65 changed files with 625 additions and 362 deletions
@@ -2,6 +2,7 @@
namespace App\Http\Controllers\V1\Admin\Server;
use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Models\Plan;
use App\Models\ServerGroup;
@@ -39,7 +40,7 @@ class GroupController extends Controller
public function save(Request $request)
{
if (empty($request->input('name'))) {
abort(500, '组名不能为空');
throw new ApiException(500, '组名不能为空');
}
if ($request->input('id')) {
@@ -59,22 +60,22 @@ class GroupController extends Controller
if ($request->input('id')) {
$serverGroup = ServerGroup::find($request->input('id'));
if (!$serverGroup) {
abort(500, '组不存在');
throw new ApiException(500, '组不存在');
}
}
$servers = ServerVmess::all();
foreach ($servers as $server) {
if (in_array($request->input('id'), $server->group_id)) {
abort(500, '该组已被节点所使用,无法删除');
throw new ApiException(500, '该组已被节点所使用,无法删除');
}
}
if (Plan::where('group_id', $request->input('id'))->first()) {
abort(500, '该组已被订阅所使用,无法删除');
throw new ApiException(500, '该组已被订阅所使用,无法删除');
}
if (User::where('group_id', $request->input('id'))->first()) {
abort(500, '该组已被用户所使用,无法删除');
throw new ApiException(500, '该组已被用户所使用,无法删除');
}
return response([
'data' => $serverGroup->delete()
@@ -2,6 +2,7 @@
namespace App\Http\Controllers\V1\Admin\Server;
use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Models\ServerHysteria;
use Illuminate\Http\Request;
@@ -44,12 +45,12 @@ class HysteriaController extends Controller
if ($request->input('id')) {
$server = ServerHysteria::find($request->input('id'));
if (!$server) {
abort(500, '服务器不存在');
throw new ApiException(500, '服务器不存在');
}
try {
$server->update($params);
} catch (\Exception $e) {
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
return response([
'data' => true
@@ -57,7 +58,7 @@ class HysteriaController extends Controller
}
if (!ServerHysteria::create($params)) {
abort(500, '创建失败');
throw new ApiException(500, '创建失败');
}
return response([
@@ -70,7 +71,7 @@ class HysteriaController extends Controller
if ($request->input('id')) {
$server = ServerHysteria::find($request->input('id'));
if (!$server) {
abort(500, '节点ID不存在');
throw new ApiException(500, '节点ID不存在');
}
}
return response([
@@ -92,12 +93,12 @@ class HysteriaController extends Controller
$server = ServerHysteria::find($request->input('id'));
if (!$server) {
abort(500, '该服务器不存在');
throw new ApiException(500, '该服务器不存在');
}
try {
$server->update($params);
} catch (\Exception $e) {
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
return response([
@@ -110,10 +111,10 @@ class HysteriaController extends Controller
$server = ServerHysteria::find($request->input('id'));
$server->show = 0;
if (!$server) {
abort(500, '服务器不存在');
throw new ApiException(500, '服务器不存在');
}
if (!ServerHysteria::create($server->toArray())) {
abort(500, '复制失败');
throw new ApiException(500, '复制失败');
}
return response([
@@ -2,6 +2,7 @@
namespace App\Http\Controllers\V1\Admin\Server;
use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Services\ServerService;
use Illuminate\Http\Request;
@@ -33,7 +34,7 @@ class ManageController extends Controller
foreach($v as $id => $sort) {
if (!$model::find($id)->update(['sort' => $sort])) {
DB::rollBack();
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
}
}
@@ -2,6 +2,7 @@
namespace App\Http\Controllers\V1\Admin\Server;
use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Models\ServerRoute;
use Illuminate\Http\Request;
@@ -47,10 +48,10 @@ class RouteController extends Controller
'data' => true
];
} catch (\Exception $e) {
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
}
if (!ServerRoute::create($params)) abort(500, '创建失败');
if (!ServerRoute::create($params)) throw new ApiException(500, '创建失败');
return [
'data' => true
];
@@ -59,8 +60,8 @@ class RouteController extends Controller
public function drop(Request $request)
{
$route = ServerRoute::find($request->input('id'));
if (!$route) abort(500, '路由不存在');
if (!$route->delete()) abort(500, '删除失败');
if (!$route) throw new ApiException(500, '路由不存在');
if (!$route->delete()) throw new ApiException(500, '删除失败');
return [
'data' => true
];
@@ -2,6 +2,7 @@
namespace App\Http\Controllers\V1\Admin\Server;
use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\ServerShadowsocksSave;
use App\Http\Requests\Admin\ServerShadowsocksUpdate;
@@ -16,12 +17,12 @@ class ShadowsocksController extends Controller
if ($request->input('id')) {
$server = ServerShadowsocks::find($request->input('id'));
if (!$server) {
abort(500, '服务器不存在');
throw new ApiException(500, '服务器不存在');
}
try {
$server->update($params);
} catch (\Exception $e) {
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
return response([
'data' => true
@@ -29,7 +30,7 @@ class ShadowsocksController extends Controller
}
if (!ServerShadowsocks::create($params)) {
abort(500, '创建失败');
throw new ApiException(500, '创建失败');
}
return response([
@@ -42,7 +43,7 @@ class ShadowsocksController extends Controller
if ($request->input('id')) {
$server = ServerShadowsocks::find($request->input('id'));
if (!$server) {
abort(500, '节点ID不存在');
throw new ApiException(500, '节点ID不存在');
}
}
return response([
@@ -59,12 +60,12 @@ class ShadowsocksController extends Controller
$server = ServerShadowsocks::find($request->input('id'));
if (!$server) {
abort(500, '该服务器不存在');
throw new ApiException(500, '该服务器不存在');
}
try {
$server->update($params);
} catch (\Exception $e) {
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
return response([
@@ -77,10 +78,10 @@ class ShadowsocksController extends Controller
$server = ServerShadowsocks::find($request->input('id'));
$server->show = 0;
if (!$server) {
abort(500, '服务器不存在');
throw new ApiException(500, '服务器不存在');
}
if (!ServerShadowsocks::create($server->toArray())) {
abort(500, '复制失败');
throw new ApiException(500, '复制失败');
}
return response([
@@ -2,6 +2,7 @@
namespace App\Http\Controllers\V1\Admin\Server;
use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\ServerTrojanSave;
use App\Http\Requests\Admin\ServerTrojanUpdate;
@@ -17,12 +18,12 @@ class TrojanController extends Controller
if ($request->input('id')) {
$server = ServerTrojan::find($request->input('id'));
if (!$server) {
abort(500, '服务器不存在');
throw new ApiException(500, '服务器不存在');
}
try {
$server->update($params);
} catch (\Exception $e) {
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
return response([
'data' => true
@@ -30,7 +31,7 @@ class TrojanController extends Controller
}
if (!ServerTrojan::create($params)) {
abort(500, '创建失败');
throw new ApiException(500, '创建失败');
}
return response([
@@ -43,7 +44,7 @@ class TrojanController extends Controller
if ($request->input('id')) {
$server = ServerTrojan::find($request->input('id'));
if (!$server) {
abort(500, '节点ID不存在');
throw new ApiException(500, '节点ID不存在');
}
}
return response([
@@ -60,12 +61,12 @@ class TrojanController extends Controller
$server = ServerTrojan::find($request->input('id'));
if (!$server) {
abort(500, '该服务器不存在');
throw new ApiException(500, '该服务器不存在');
}
try {
$server->update($params);
} catch (\Exception $e) {
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
return response([
@@ -78,10 +79,10 @@ class TrojanController extends Controller
$server = ServerTrojan::find($request->input('id'));
$server->show = 0;
if (!$server) {
abort(500, '服务器不存在');
throw new ApiException(500, '服务器不存在');
}
if (!ServerTrojan::create($server->toArray())) {
abort(500, '复制失败');
throw new ApiException(500, '复制失败');
}
return response([
@@ -2,6 +2,7 @@
namespace App\Http\Controllers\V1\Admin\Server;
use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Models\ServerVless;
use Illuminate\Http\Request;
@@ -61,12 +62,12 @@ class VlessController extends Controller
if ($request->input('id')) {
$server = ServerVless::find($request->input('id'));
if (!$server) {
abort(500, '服务器不存在');
throw new ApiException(500, '服务器不存在');
}
try {
$server->update($params);
} catch (\Exception $e) {
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
return response([
'data' => true
@@ -74,7 +75,7 @@ class VlessController extends Controller
}
if (!ServerVless::create($params)) {
abort(500, '创建失败');
throw new ApiException(500, '创建失败');
}
return response([
@@ -87,7 +88,7 @@ class VlessController extends Controller
if ($request->input('id')) {
$server = ServerVless::find($request->input('id'));
if (!$server) {
abort(500, '节点ID不存在');
throw new ApiException(500, '节点ID不存在');
}
}
return response([
@@ -104,12 +105,12 @@ class VlessController extends Controller
$server = ServerVless::find($request->input('id'));
if (!$server) {
abort(500, '该服务器不存在');
throw new ApiException(500, '该服务器不存在');
}
try {
$server->update($params);
} catch (\Exception $e) {
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
return response([
@@ -122,10 +123,10 @@ class VlessController extends Controller
$server = ServerVless::find($request->input('id'));
$server->show = 0;
if (!$server) {
abort(500, '服务器不存在');
throw new ApiException(500, '服务器不存在');
}
if (!ServerVless::create($server->toArray())) {
abort(500, '复制失败');
throw new ApiException(500, '复制失败');
}
return response([
@@ -2,6 +2,7 @@
namespace App\Http\Controllers\V1\Admin\Server;
use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\ServerVmessSave;
use App\Http\Requests\Admin\ServerVmessUpdate;
@@ -17,12 +18,12 @@ class VmessController extends Controller
if ($request->input('id')) {
$server = ServerVmess::find($request->input('id'));
if (!$server) {
abort(500, '服务器不存在');
throw new ApiException(500, '服务器不存在');
}
try {
$server->update($params);
} catch (\Exception $e) {
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
return response([
'data' => true
@@ -30,7 +31,7 @@ class VmessController extends Controller
}
if (!ServerVmess::create($params)) {
abort(500, '创建失败');
throw new ApiException(500, '创建失败');
}
return response([
@@ -43,7 +44,7 @@ class VmessController extends Controller
if ($request->input('id')) {
$server = ServerVmess::find($request->input('id'));
if (!$server) {
abort(500, '节点ID不存在');
throw new ApiException(500, '节点ID不存在');
}
}
return response([
@@ -60,12 +61,12 @@ class VmessController extends Controller
$server = ServerVmess::find($request->input('id'));
if (!$server) {
abort(500, '该服务器不存在');
throw new ApiException(500, '该服务器不存在');
}
try {
$server->update($params);
} catch (\Exception $e) {
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
return response([
@@ -78,10 +79,10 @@ class VmessController extends Controller
$server = ServerVmess::find($request->input('id'));
$server->show = 0;
if (!$server) {
abort(500, '服务器不存在');
throw new ApiException(500, '服务器不存在');
}
if (!ServerVmess::create($server->toArray())) {
abort(500, '复制失败');
throw new ApiException(500, '复制失败');
}
return response([