diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index c18c43c..a6d5381 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -3,7 +3,9 @@ namespace App\Exceptions; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Throwable; +use Illuminate\Validation\ValidationException; class Handler extends ExceptionHandler { @@ -38,4 +40,41 @@ class Handler extends ExceptionHandler // }); } + + /** + * 报告异常 + * @return bool|void + */ + public function report(Throwable $e): bool + { + // 判断异常是否需要自定义报告... + return false; + } + + public function render($request, Throwable $e) + { + // 表单校验 + if ($e instanceof ValidationException) { + $errors = $e->errors(); + while (is_array($errors)) { + $errors = current($errors); + } + return response()->json([ + 'code' => -1, + 'msg' => $errors, + 'data' => [], + 'status' => 500, + ]); + } + // 404 + if ($e instanceof NotFoundHttpException) { + return response()->json([ + 'code' => -1, + 'msg' => '404 Not Found', + 'data' => [], + 'status' => 500, + ]); + } + return parent::render($request, $e); + } }