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,11 +2,11 @@
namespace App\Http\Controllers\V1\Staff;
use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\NoticeSave;
use App\Models\Notice;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
class NoticeController extends Controller
{
@@ -26,13 +26,13 @@ class NoticeController extends Controller
]);
if (!$request->input('id')) {
if (!Notice::create($data)) {
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
} else {
try {
Notice::find($request->input('id'))->update($data);
} catch (\Exception $e) {
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
}
return response([
@@ -43,14 +43,14 @@ class NoticeController extends Controller
public function drop(Request $request)
{
if (empty($request->input('id'))) {
abort(500, '参数错误');
throw new ApiException(422, '参数错误');
}
$notice = Notice::find($request->input('id'));
if (!$notice) {
abort(500, '公告不存在');
throw new ApiException(500, '公告不存在');
}
if (!$notice->delete()) {
abort(500, '删除失败');
throw new ApiException(500, '删除失败');
}
return response([
'data' => true
@@ -2,6 +2,7 @@
namespace App\Http\Controllers\V1\Staff;
use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Models\Ticket;
use App\Models\TicketMessage;
@@ -16,7 +17,7 @@ class TicketController extends Controller
$ticket = Ticket::where('id', $request->input('id'))
->first();
if (!$ticket) {
abort(500, '工单不存在');
throw new ApiException(500, '工单不存在');
}
$ticket['message'] = TicketMessage::where('ticket_id', $ticket->id)->get();
for ($i = 0; $i < count($ticket['message']); $i++) {
@@ -48,10 +49,10 @@ class TicketController extends Controller
public function reply(Request $request)
{
if (empty($request->input('id'))) {
abort(500, '参数错误');
throw new ApiException(422, '参数错误');
}
if (empty($request->input('message'))) {
abort(500, '消息不能为空');
throw new ApiException(500, '消息不能为空');
}
$ticketService = new TicketService();
$ticketService->replyByAdmin(
@@ -67,16 +68,16 @@ class TicketController extends Controller
public function close(Request $request)
{
if (empty($request->input('id'))) {
abort(500, '参数错误');
throw new ApiException(422, '参数错误');
}
$ticket = Ticket::where('id', $request->input('id'))
->first();
if (!$ticket) {
abort(500, '工单不存在');
throw new ApiException(500, '工单不存在');
}
$ticket->status = 1;
if (!$ticket->save()) {
abort(500, '关闭失败');
throw new ApiException(500, '关闭失败');
}
return response([
'data' => true
@@ -2,6 +2,7 @@
namespace App\Http\Controllers\V1\Staff;
use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\UserSendMail;
use App\Http\Requests\Staff\UserUpdate;
@@ -15,13 +16,13 @@ class UserController extends Controller
public function getUserInfoById(Request $request)
{
if (empty($request->input('id'))) {
abort(500, '参数错误');
throw new ApiException(422, '参数错误');
}
$user = User::where('is_admin', 0)
->where('id', $request->input('id'))
->where('is_staff', 0)
->first();
if (!$user) abort(500, '用户不存在');
if (!$user) throw new ApiException(500, '用户不存在');
return response([
'data' => $user
]);
@@ -32,10 +33,10 @@ class UserController extends Controller
$params = $request->validated();
$user = User::find($request->input('id'));
if (!$user) {
abort(500, '用户不存在');
throw new ApiException(500, '用户不存在');
}
if (User::where('email', $params['email'])->first() && $user->email !== $params['email']) {
abort(500, '邮箱已被使用');
throw new ApiException(500, '邮箱已被使用');
}
if (isset($params['password'])) {
$params['password'] = password_hash($params['password'], PASSWORD_DEFAULT);
@@ -46,7 +47,7 @@ class UserController extends Controller
if (isset($params['plan_id'])) {
$plan = Plan::find($params['plan_id']);
if (!$plan) {
abort(500, '订阅计划不存在');
throw new ApiException(500, '订阅计划不存在');
}
$params['group_id'] = $plan->group_id;
}
@@ -54,7 +55,7 @@ class UserController extends Controller
try {
$user->update($params);
} catch (\Exception $e) {
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
return response([
'data' => true
@@ -97,7 +98,7 @@ class UserController extends Controller
'banned' => 1
]);
} catch (\Exception $e) {
abort(500, '处理失败');
throw new ApiException(500, '处理失败');
}
return response([