【问题标题】:Laravel return response() not stop the executionLaravel 返回 response() 不会停止执行
【发布时间】:2020-04-17 19:27:16
【问题描述】:

我无法解释为什么 catch 中的 return response() 没有停止执行,我想我错过了一些东西。

在此示例中,请求有错误,因此服务上的 try-catch 接收到ValidationException。这转到ValidationErrorResponder,这里是执行应该完成并返回带有错误的json的地方。但它继续并通过$this->updateUserResponder->respond()返回json错误响应

我有一个已定义的路由,它在UpdateUserAction 上执行__invoke() 方法

class UpdateUserAction
{
    protected $updateUserService;

    protected $updateUserResponder;

    public function __construct(UpdateUserService $updateUserService, UpdateUserResponder $updateUserResponder)
    {
        $this->updateUserService = $updateUserService;
        $this->updateUserResponder = $updateUserResponder;
    }

    public function __invoke(Request $request, $userId)
    {
        $serviceData = [
            'id' => $userId,
            'commandPayload' => $request->only('name')
        ];

        return $this->updateUserResponder->respond($this->updateUserService->execute($serviceData));
    }
}
class UpdateUserService extends BaseService
{
    public function execute(array $data = [])
    {
        try {
            $this->bus->addHandler(UpdateUserCommand::class, UpdateUserHandler::class);
            return $this->bus->dispatch(UpdateUserCommand::class, $data, [UpdateUserValidator::class]);
        } catch (ValidationException $e) {
            return $this->validationErrorResponder->respond($e);
        }
    }
}
class UpdateUserValidator implements Middleware
{
    protected $rules = [
        'id' => 'uuid',
        'commandPayload.name' => 'max:256'
    ];

    protected $messages = [];

    public function execute($command, callable $next)
    {
        $validator = Validator::make((array) $command, $this->rules, $this->messages);

        if ($validator->fails()) {
            throw new ValidationException($validator);
        }
        return $next($command);
    }
}

这应该返回带有 JSON 错误的最终响应,但是

class ValidationErrorResponder
{
    public function respond($validator)
    {
        $messages = $validator->getValidator()->getMessageBag()->messages();

        return response()->json(['errors' => $messages], 422);
    }
}

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    也许是另一个错误,并且捕获不起作用,因为只捕获 ValidationException

    所以尝试捕获所有异常,看看会发生什么:

    class UpdateUserService extends BaseService
    {
        public function execute(array $data = [])
        {
            try {
                $this->bus->addHandler(UpdateUserCommand::class, UpdateUserHandler::class);
                return $this->bus->dispatch(UpdateUserCommand::class, $data, [UpdateUserValidator::class]);
            } catch (\Exception $e) {
                return $this->validationErrorResponder->respond($e);
            }
        }
    }

    【讨论】:

    • catch 工作正常,因为 $this->validationErrorResponder->respond($e); 已执行。
    猜你喜欢
    • 2022-01-24
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多