更新异常处理器

dev
Jing Li 2023-10-25 10:55:08 +08:00
parent e13ca5cf4b
commit 342c5f7c3d
1 changed files with 10 additions and 3 deletions

View File

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