From 342c5f7c3d3405822b64eef94e6e2dbd50067ca6 Mon Sep 17 00:00:00 2001 From: Jing Li Date: Wed, 25 Oct 2023 10:55:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=BC=82=E5=B8=B8=E5=A4=84?= =?UTF-8?q?=E7=90=86=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Exceptions/Handler.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index d008bf5..fc29c67 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; use Illuminate\Http\Exceptions\ThrottleRequestsException; use Illuminate\Http\Request; +use Illuminate\Support\Arr; use Illuminate\Validation\ValidationException; use Symfony\Component\HttpKernel\Exception\HttpException; use Throwable; @@ -111,10 +112,16 @@ class Handler extends ExceptionHandler */ protected function invalidJson($request, ValidationException $exception) { + $errors = $exception->errors(); + + if (is_array($firstMessage = Arr::first($errors))) { + $firstMessage = Arr::first($firstMessage); + } + return response()->json([ - 'code' => 422, - 'message' => $exception->getMessage(), - 'errors' => $exception->errors(), + 'errcode' => 422, + 'errmsg' => $firstMessage ?: '参数错误', + 'errors' => $errors, ], 200); }