diff --git a/app/Admin/Controllers/AdminController.php b/app/Admin/Controllers/AdminController.php index ef2bc15..6a2daaf 100644 --- a/app/Admin/Controllers/AdminController.php +++ b/app/Admin/Controllers/AdminController.php @@ -41,7 +41,7 @@ abstract class AdminController extends Controller DB::commit(); } catch (Throwable $th) { DB::rollBack(); - throw $this->prepareException($th); + return $this->renderException($th); } return $this->response()->successMessage(__('admin.save').__('admin.successfully')); @@ -70,7 +70,7 @@ abstract class AdminController extends Controller DB::commit(); } catch (Throwable $th) { DB::rollBack(); - throw $this->prepareException($th); + return $this->renderException($th); } return $this->response()->successMessage(__('admin.save').__('admin.successfully')); @@ -89,7 +89,7 @@ abstract class AdminController extends Controller DB::commit(); } catch (Throwable $th) { DB::rollBack(); - throw $this->prepareException($th); + return $this->renderException($th); } return $this->response()->successMessage(__('admin.delete').__('admin.successfully')); @@ -106,16 +106,23 @@ abstract class AdminController extends Controller return $path; } - protected function prepareException(Throwable $e) + protected function renderException(Throwable $e) { + report($e); + + if ($e instanceof AdminException) { + return $e->render(); + } + + $message = $e->getMessage(); + if ($e instanceof ValidationException) { $message = Arr::first($e->errors()); if (is_array($message)) { - $message = Arr::first($message); + $message = Arr::first($message) ?: '参数错误'; } - $e = new AdminException($message ?: '参数错误'); } - return $e; + return $this->response()->fail($message); } } diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 5cc2140..6bfc524 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -6,6 +6,7 @@ use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; use Throwable; use Illuminate\Http\{Request}; use Illuminate\Validation\ValidationException; +use Slowlyo\OwlAdmin\Exceptions\AdminException; class Handler extends ExceptionHandler { @@ -21,6 +22,7 @@ class Handler extends ExceptionHandler ]; protected $dontReport = [ + AdminException::class, RuntimeException::class, ];