【问题标题】:Laravel Good Practices with Exceptions and Try&Catch Blocks带有异常和 Try&Catch 块的 Laravel 良好实践
【发布时间】:2015-11-03 18:30:08
【问题描述】:

我正在构建一个网络应用程序,客户可以在其中购买许多不同的计划,并且我正在使用 Stripe API 进行付款。当客户想要购买计划时,还必须填写信用卡详细信息和电子邮件。所以,我在我的 RegistrationController 中获取了所有这些表单数据。

问题是,我必须在 post 方法中做很多事情,例如:

  • 检查所选计划是否存在(也许有人可以破解 html 表单源)。
  • 创建条纹服装。
  • 为我创建的客户创建 Stripe 订阅。
  • 创建新的 Eloquent 用户,同步选择的计划并将 Stripe 信息(Stripe id 等)添加到用户实例。

由于我必须执行许多步骤,因此我决定使用 Try&Catch 块并创建自定义异常,因此,如果出现故障,我将能够跟踪错误发生的位置。问题是我在 RegistrationController 中以一个凌乱的方法结束:

public function postRegistration(RegistrationRequest $request,
                                 StripeCostumer $stripeCustomer,
                                 StripeSubscription $stripeSubscription)
{
    if ($request['training_plan'])
    {

        if ( ! $this->PlanExists($request['training_plan']))
        {
            \Log::alert('Somebody tried to hack the plan: '. 
            $request['email']);

            return response()->json(
                ['error' => \Config::get('variables.104')],
                Response::HTTP_NOT_FOUND);
        }
    }
    try
    {
        $response = $stripeCustomer->createNewStripeCostumer($request);

        $plans = $stripeSubscription->createNewStripeSubscription($response->id, $request);

        $user = $this->userRepo->create($request->all());

        $user->syncUserPlans($plans);

        $this->userRepo->saveStripeInfo($user,$response);

    }
    catch(StripeCustomerNotCreated $e)
    {
        \Log::error('Couldn't create a new Stripe Costumer: '.
            $request['email']);

        return response()->json(
            ['error' => \Config::get('variables.106')],
            Response::HTTP_PAYMENT_REQUIRED);

    }
    catch(StripeSubscriptionNotCreated $e)
    ...
    catch(EloquentUserNotCreated $e)
    ...
    catch(StripeInfoNotSaved $e)
    ...

    event(new UserRegistration($user));

    \Auth::login($user);

    return redirect('/');
}

我没有编写每个 Catch 块(我目前有 4-5 个),但每次抛出异常时,我都必须:

  • 撤消之前的所有操作(创建 Stripe Customer、Eloquent User 等),因此在每个 Catch 中逻辑都会变得更大。
  • 记录事件。
  • 返回错误。

这是管理 Stripe 客户的服务类方法的示例:

public function createNewStripeCustomer($request)
{
    $response = Customer::create(array(
        "description" => "Customer for test@example.com",
        "source" => $request->stripeToken,
        "email" => $request->email,
    ));

    if(true)
    {
        return $response;
    }

    throw new StripeCustomerNotCreated();
}

*如果有任何错误,我会像 API 一样返回 JSON。

*我在 /Config 目录中有“variables.php”文件,用于保存所有错误消息。

我尝试将每个异常的逻辑保存在 Handler.php 文件中(使用 switch 循环),但它没有按我预期的那样工作。其他选项是替换许多 if&else 或嵌套 try&catch 块的 try&catch 块,但它仍然很乱。

使这项工作有效进行的最佳方法应该是什么?

【问题讨论】:

    标签: performance laravel try-catch


    【解决方案1】:

    我终于在 Handler.php 中找到了处理 Stripe 异常(不是我的自定义异常)的解决方案。我发现这个post 可能对某人有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-10
      • 2012-09-20
      • 1970-01-01
      • 1970-01-01
      • 2015-12-10
      • 1970-01-01
      • 2019-01-18
      • 2019-07-12
      相关资源
      最近更新 更多