更新Laravel 5.4。 5.5打破验证响应

时间:2018-02-06 21:02:32

标签: laravel laravel-5.4 laravel-5.5

我在laravel 5.4上运行了一个API,我想将其更新为5.5 我用“laravel / framework”更新了composer.json:“5.5。*”和“phpunit / phpunit”:“〜6.0”

问题在于,现在每当出现验证错误时,我总是得到状态代码为500,并显示消息“给定数据无效”,而不是实际错误和我之前使用laravel 5.4获得的相应状态代码< / p>

我在更新期间是否遗漏了一些内容,我还必须更改以使其工作?

Response : Array
(
    [message] => The given data was invalid.
    [status_code] => 500
    [debug] => Array
        (
            [line] => 306
            [file] => E:\Projects\overwatch\overwatch-backend\vendor\laravel\framework\src\Illuminate\Validation\Validator.php
            [class] => Illuminate\Validation\ValidationException
            [trace] => Array
                (
                    [0] => #0 E:\Projects\overwatch\overwatch-backend\vendor\laravel\framework\src\Illuminate\Foundation\Validation\ValidatesRequests.php(46): Illuminate\Validation\Validator->validate()
                    [1] => #1 E:\Projects\overwatch\overwatch-backend\app\Http\Controllers\Api\AuthController.php(58): App\Http\Controllers\Controller->validate(Object(Dingo\Api\Http\Request), Array)
                    [2] => #2 E:\Projects\overwatch\overwatch-backend\app\Http\Controllers\Api\AuthController.php(44): App\Http\Controllers\Api\AuthController->authenticateUser(Object(Dingo\Api\Http\Request))
                    [3] => #3 [internal function]: App\Http\Controllers\Api\AuthController->login(Object(Dingo\Api\Http\Request))
                    [4] => #4 E:\Projects\overwatch\overwatch-backend\vendor\laravel\framework\src\Illuminate\Routing\Controller.php(54): call_user_func_array(Array, Array)
                    [5] => #5 E:\Projects\overwatch\overwatch-backend\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php(45): Illuminate\Routing\Controller->callAction('login', Array)
                    [6] => #6 E:\Projects\overwatch\overwatch-backend\vendor\laravel\framework\src\Illuminate\Routing\Route.php(212): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(App\Http\Controllers\Api\AuthController), 'login')
                    [7] => #7 E:\Projects\overwatch\overwatch-backend\vendor\laravel\framework\src\Illuminate\Routing\Route.php(169): Illuminate\Routing\Route->runController()
                    [8] => #8 E:\Projects\overwatch\overwatch-backend\vendor\laravel\framework\src\Illuminate\Routing\Router.php(658): Illuminate\Routing\Route->run()
                    [9] => #9 E:\Projects\overwatch\overwatch-backend\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(30): Illuminate\Routing\Router->Illuminate\Routing\{closure}(Object(Dingo\Api\Http\Request))
                    [10] => #10 E:\Projects\overwatch\overwatch-backend\vendor\dingo\api\src\Http\Middleware\PrepareController.php(45): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Dingo\Api\Http\Request))
                    [11] => #11 E:\Projects\overwatch\overwatch-backend\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(149): Dingo\Api\Http\Middleware\PrepareController->handle(Object(Dingo\Api\Http\Request), Object(Closure))
                    [12] => #12 E:\Projects\overwatch\overwatch-backend\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Dingo\Api\Http\Request))
                    [13] => #13 E:\Projects\overwatch\overwatch-backend\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(102): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Dingo\Api\Http\Request))
                    [14] => #14 E:\Projects\overwatch\overwatch-backend\vendor\laravel\framework\src\Illuminate\Routing\Router.php(660): Illuminate\Pipeline\Pipeline->then(Object(Closure))
                    [15] => #15 E:\Projects\overwatch\overwatch-backend\vendor\laravel\framework\src\Illuminate\Routing\Router.php(635): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Dingo\Api\Http\Request))
                    [16] => #16 E:\Projects\overwatch\overwatch-backend\vendor\laravel\framework\src\Illuminate\Routing\Router.php(601): Illuminate\Routing\Router->runRoute(Object(Dingo\Api\Http\Request), Object(Illuminate\Routing\Route))
                    [17] => #17 E:\Projects\overwatch\overwatch-backend\vendor\laravel\framework\src\Illuminate\Routing\Router.php(590): Illuminate\Routing\Router->dispatchToRoute(Object(Dingo\Api\Http\Request))
                    [18] => #18 E:\Projects\overwatch\overwatch-backend\vendor\dingo\api\src\Routing\Adapter\Laravel.php(81): Illuminate\Routing\Router->dispatch(Object(Dingo\Api\Http\Request))
                    [19] => #19 E:\Projects\overwatch\overwatch-backend\vendor\dingo\api\src\Routing\Router.php(513): Dingo\Api\Routing\Adapter\Laravel->dispatch(Object(Dingo\Api\Http\Request), 'v1')
                    [20] => #20 E:\Projects\overwatch\overwatch-backend\vendor\dingo\api\src\Http\Middleware\Request.php(126): Dingo\Api\Routing\Router->dispatch(Object(Dingo\Api\Http\Request))
                    [21] => #21 E:\Projects\overwatch\overwatch-backend\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(114): Dingo\Api\Http\Middleware\Request->Dingo\Api\Http\Middleware\{closure}(Object(Dingo\Api\Http\Request))
                    [22] => #22 E:\Projects\overwatch\overwatch-backend\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php(30): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Dingo\Api\Http\Request))
                    [23] => #23 E:\Projects\overwatch\overwatch-backend\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(149): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Dingo\Api\Http\Request), Object(Closure))
                    [24] => #24 E:\Projects\overwatch\overwatch-backend\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php(30): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Dingo\Api\Http\Request))
                    [25] => #25 E:\Projects\overwatch\overwatch-backend\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(149): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Dingo\Api\Http\Request), Object(Closure))
                    [26] => #26 E:\Projects\overwatch\overwatch-backend\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\ValidatePostSize.php(27): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Dingo\Api\Http\Request))
                    [27] => #27 E:\Projects\overwatch\overwatch-backend\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(149): Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle(Object(Dingo\Api\Http\Request), Object(Closure))
                    [28] => #28 E:\Projects\overwatch\overwatch-backend\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode.php(46): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Dingo\Api\Http\Request))
                    [29] => #29 E:\Projects\overwatch\overwatch-backend\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(149): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Dingo\Api\Http\Request), Object(Closure))
                    [30] => #30 E:\Projects\overwatch\overwatch-backend\vendor\barryvdh\laravel-cors\src\HandlePreflight.php(46): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Dingo\Api\Http\Request))
                    [31] => #31 E:\Projects\overwatch\overwatch-backend\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(149): Barryvdh\Cors\HandlePreflight->handle(Object(Dingo\Api\Http\Request), Object(Closure))
                    [32] => #32 E:\Projects\overwatch\overwatch-backend\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(102): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Dingo\Api\Http\Request))
                    [33] => #33 E:\Projects\overwatch\overwatch-backend\vendor\dingo\api\src\Http\Middleware\Request.php(127): Illuminate\Pipeline\Pipeline->then(Object(Closure))
                    [34] => #34 E:\Projects\overwatch\overwatch-backend\vendor\dingo\api\src\Http\Middleware\Request.php(103): Dingo\Api\Http\Middleware\Request->sendRequestThroughRouter(Object(Dingo\Api\Http\Request))
                    [35] => #35 E:\Projects\overwatch\overwatch-backend\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(149): Dingo\Api\Http\Middleware\Request->handle(Object(Dingo\Api\Http\Request), Object(Closure))
                    [36] => #36 E:\Projects\overwatch\overwatch-backend\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
                    [37] => #37 E:\Projects\overwatch\overwatch-backend\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(102): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
                    [38] => #38 E:\Projects\overwatch\overwatch-backend\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(151): Illuminate\Pipeline\Pipeline->then(Object(Closure))
                    [39] => #39 E:\Projects\overwatch\overwatch-backend\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(116): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
                    [40] => #40 E:\Projects\overwatch\overwatch-backend\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests.php(345): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
                    [41] => #41 E:\Projects\overwatch\overwatch-backend\tests\ApiTester.php(90): Illuminate\Foundation\Testing\TestCase->call('POST', 'api/v1/login/', Array, Array, Array, Array)
                    [42] => #42 E:\Projects\overwatch\overwatch-backend\tests\Unit\Api\LoginTest.php(38): Tests\ApiTester->request('POST', Array)
                    [43] => #43 [internal function]: Tests\loginTest->test_login_with_missing_credentials()
                    [44] => #44 E:\Projects\overwatch\overwatch-backend\vendor\phpunit\phpunit\src\Framework\TestCase.php(1071): ReflectionMethod->invokeArgs(Object(Tests\loginTest), Array)
                    [45] => #45 E:\Projects\overwatch\overwatch-backend\vendor\phpunit\phpunit\src\Framework\TestCase.php(939): PHPUnit\Framework\TestCase->runTest()
                    [46] => #46 E:\Projects\overwatch\overwatch-backend\vendor\phpunit\phpunit\src\Framework\TestResult.php(698): PHPUnit\Framework\TestCase->runBare()
                    [47] => #47 E:\Projects\overwatch\overwatch-backend\vendor\phpunit\phpunit\src\Framework\TestCase.php(894): PHPUnit\Framework\TestResult->run(Object(Tests\loginTest))
                    [48] => #48 E:\Projects\overwatch\overwatch-backend\vendor\phpunit\phpunit\src\Framework\TestSuite.php(755): PHPUnit\Framework\TestCase->run(Object(PHPUnit\Framework\TestResult))
                    [49] => #49 E:\Projects\overwatch\overwatch-backend\vendor\phpunit\phpunit\src\Framework\TestSuite.php(755): PHPUnit\Framework\TestSuite->run(Object(PHPUnit\Framework\TestResult))
                    [50] => #50 E:\Projects\overwatch\overwatch-backend\vendor\phpunit\phpunit\src\Framework\TestSuite.php(755): PHPUnit\Framework\TestSuite->run(Object(PHPUnit\Framework\TestResult))
                    [51] => #51 E:\Projects\overwatch\overwatch-backend\vendor\phpunit\phpunit\src\TextUI\TestRunner.php(546): PHPUnit\Framework\TestSuite->run(Object(PHPUnit\Framework\TestResult))
                    [52] => #52 E:\Projects\overwatch\overwatch-backend\vendor\phpunit\phpunit\src\TextUI\Command.php(195): PHPUnit\TextUI\TestRunner->doRun(Object(PHPUnit\Framework\TestSuite), Array, true)
                    [53] => #53 E:\Projects\overwatch\overwatch-backend\vendor\phpunit\phpunit\src\TextUI\Command.php(148): PHPUnit\TextUI\Command->run(Array, true)
                    [54] => #54 E:\Projects\overwatch\overwatch-backend\vendor\phpunit\phpunit\phpunit(53): PHPUnit\TextUI\Command::main()
                    [55] => #55 {main}
                )

        )

)

1 个答案:

答案 0 :(得分:2)

在laravel 5.5中,所有异常都转换为HTTP响应。如果您想以旧样式(JSON错误格式)维护它,您应该将以下方法添加到App\Exceptions\Handler类。

use Illuminate\Validation\ValidationException;

protected function invalidJson($request, ValidationException $exception)
{
    return response()->json($exception->errors(), $exception->status);
}

view docs