From 9dfcaf4b1034a22d5b103125b355691f6618e440 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E4=B8=91=E8=B7=AF=E4=BA=BA?= <2278757482@qq.com> Date: Wed, 29 Sep 2021 09:40:25 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E7=9B=91=E5=90=AC=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Exceptions/Handler.php | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 48a5099..e3e9edc 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -3,7 +3,10 @@ namespace App\Exceptions; use App\Traits\Json; +use Illuminate\Validation\ValidationException; +use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Throwable; class Handler extends ExceptionHandler @@ -19,8 +22,8 @@ class Handler extends ExceptionHandler \Illuminate\Auth\AuthenticationException::class, \Illuminate\Auth\Access\AuthorizationException::class, \Symfony\Component\HttpKernel\Exception\HttpException::class, - \Illuminate\Database\Eloquent\ModelNotFoundException::class, - \Illuminate\Validation\ValidationException::class, + ModelNotFoundException::class, + ValidationException::class, ]; /** @@ -57,11 +60,30 @@ class Handler extends ExceptionHandler */ public function render($request, Throwable $exception) { + // 路由404异常监听 + if($exception instanceof NotFoundHttpException){ + $this->setHttpCode(404); + return $this->errorJson("路由{{$request->path()}}不存在!"); + } + + // 模型不存在 + if ($exception instanceof ModelNotFoundException){ + return $this->errorJson($exception->getMessage()); + } + // 验证器类的错误监听 - if($exception instanceof \Illuminate\Validation\ValidationException){ + if($exception instanceof ValidationException){ return $this->errorJson($exception->validator->errors()->first()); } + // Exception类的错误监听 + if($exception instanceof \Exception){ + return $this->errorJson($exception->getMessage(), $exception->getCode(), [], [ + 'file' => $exception->getFile(), + 'line' => $exception->getLine(), + ]); + } + return parent::render($request, $exception); } } -- Gitee From d8b4b923f1cc67f595d8a1a4420ef4d3c9b93195 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E4=B8=91=E8=B7=AF=E4=BA=BA?= <2278757482@qq.com> Date: Wed, 13 Oct 2021 09:38:42 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=BC=82=E5=B8=B8=E7=9B=91=E5=90=AC?= =?UTF-8?q?=E7=B1=BB=EF=BC=9A=E7=9B=91=E5=90=AC=E6=8E=A7=E5=88=B6=E5=99=A8?= =?UTF-8?q?=E4=B8=8D=E5=AD=98=E5=9C=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Exceptions/Handler.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index e3e9edc..b25e211 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -3,6 +3,7 @@ namespace App\Exceptions; use App\Traits\Json; +use Illuminate\Contracts\Container\BindingResolutionException; use Illuminate\Validation\ValidationException; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; @@ -66,6 +67,11 @@ class Handler extends ExceptionHandler return $this->errorJson("路由{{$request->path()}}不存在!"); } + // 控制器不存在 + if ($exception instanceof BindingResolutionException){ + return $this->errorJson($exception->getMessage()); + } + // 模型不存在 if ($exception instanceof ModelNotFoundException){ return $this->errorJson($exception->getMessage()); -- Gitee