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); }