jiannei / laravel-response

🤖 Provide a standardized and unified response data structure for Laravel and Lumen API projects. - 为 Laravel 和 Lumen API 项目提供一个规范统一的响应数据结构。
MIT License
243 stars 32 forks source link

laravel9表单验证时显示验证错误信息报错 #67

Closed HungKin closed 2 years ago

HungKin commented 2 years ago

错误描述: 表单验证不符合验证规则时,会出现以下错误:

{
    "status": "fail",
    "code": 500,
    "message": "Server Error",
    "data": {},
    "error": {
        "message": "Jiannei\\Response\\Laravel\\Response::fail(): Argument #2 ($code) must be of type int, array given, called in D:\\project\\example-app\\vendor\\laravel\\framework\\src\\Illuminate\\Support\\Facades\\Facade.php on line 337",
        "exception": "TypeError",
        "file": "D:\\project\\example-app\\vendor\\jiannei\\laravel-response\\src\\Response.php",
        "line": 158
    }
}

复现步骤:

  1. app/Exceptions/Handler.php 中引入 use Jiannei\Response\Laravel\Support\Traits\ExceptionTrait;
  2. 在方法中创建一个验证
    
    use Illuminate\Http\Request;

public function test(Request $request){ $validated = $request->validate([ 'phone' => ['required'] ]); }

jiannei commented 2 years ago

config/response.php 中的配置贴出来看一看

HungKin commented 2 years ago
<?php

/*
 * This file is part of the Jiannei/laravel-response.
 *
 * (c) Jiannei <longjian.huang@foxmail.com>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

return [
    /*
    |--------------------------------------------------------------------------
    | Set the http status code when the response fails
    |--------------------------------------------------------------------------
    |
    | the reference options are false, 200, 500
    |
    | false, stricter http status codes such as 404, 401, 403, 500, etc. will be returned
    | 200, All failed responses will also return a 200 status code
    | 500, All failed responses return a 500 status code
    */

    'error_code' => 200,

    // You can use enumerations to define the code when the response is returned,
    // and set the response message according to the locale
    //
    // The following two enumeration packages are good choices
    //
    // https://github.com/Jiannei/laravel-enum
    // https://github.com/BenSampo/laravel-enum

    'enum' => '', // \Jiannei\Enum\Laravel\Repositories\Enums\HttpStatusCodeEnum::class

    //  You can set some attributes (eg:code/message/header/options) for the exception, and it will override the default attributes of the exception
    'exception' => [
        \Illuminate\Validation\ValidationException::class => [
            'code' => 422,
        ],
        \Illuminate\Auth\AuthenticationException::class => [

        ],
        \Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class =>[
            'message' => '',
        ],
        \Illuminate\Database\Eloquent\ModelNotFoundException::class => [
            'message' => '',
        ],
    ],

    // Set the structure of the response data
    'format' => [
        'fields' => [
            'status' => ['alias' => 'status', 'show' => true],
            'code' => ['alias' => 'code', 'show' => true],
            'message' => ['alias' => 'message', 'show' => true],
            'error' => ['alias' => 'error', 'show' => true],
            'data' => [
                'alias' => 'data',
                'show' => true,

                'fields' => [
                    // When data is nested with data, such as returning paged data, you can also set an alias for the inner data
                    'data' => ['alias' => 'data', 'show' => true], // data/rows/list

                    'meta' => [
                        'alia' => 'meta',
                        'show' => true,

                        'fields' => [
                            'pagination' => [
                                'alias' => 'pagination',
                                'show' => true,

                                'fields' => [
                                    'total' => ['alias' => 'total', 'show' => true],
                                    'count' => ['alias' => 'count', 'show' => true],
                                    'per_page' => ['alias' => 'per_page', 'show' => true],
                                    'current_page' => ['alias' => 'current_page', 'show' => true],
                                    'total_pages' => ['alias' => 'total_pages', 'show' => true],
                                    'links' => [
                                        'alias' => 'links',
                                        'show' => true,

                                        'fields' => [
                                            'previous' => ['alias' => 'previous', 'show' => true],
                                            'next' => ['alias' => 'next', 'show' => true],
                                        ],
                                    ],
                                ],
                            ],
                        ],
                    ],
                ],
            ],
        ],
    ],
];
jiannei commented 2 years ago

没有复现,你可以邮件方式发送代码给我,或者发一个能够复现这问题的代码仓库链接,我来调试看看 👀

HungKin commented 2 years ago

我用家里那台测试了没有问题,应该是我环境或者代码有误,不好意思

HungKin commented 2 years ago

公司的工作机最新引用的是4.0.0,配置文件 \Illuminate\Validation\ValidationException::class 为数组,但是返回 ExceptionTraitinvalidJson 方法把整个数组返回了,升级到4.2.0就可以了