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\Admin;
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
{
@@ -27,13 +27,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([
@@ -46,15 +46,15 @@ class NoticeController extends Controller
public function show(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, '公告不存在');
}
$notice->show = $notice->show ? 0 : 1;
if (!$notice->save()) {
abort(500, '保存失败');
throw new ApiException(500, '保存失败');
}
return response([
@@ -65,14 +65,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