代码之家  ›  专栏  ›  技术社区  ›  PolarisTLX

如何检查是否存在条带错误/如果存在则放置条带错误

  •  0
  • PolarisTLX  · 技术社区  · 7 年前

    在我的Rails应用程序中,我只尝试在不存在条带错误的情况下运行条带充电命令。我希望能够检查条带错误。

    charge = Stripe::Charge.create(
      :amount => amount_in_cents,
      :customer => stripe_customer,
      :description => "my application",
    )
    

    应该是这样的:

    unless Stripe.errors.exists? do
       charge = Stripe::Charge.create(
          :amount => amount_in_cents,
          :customer => stripe_customer,
          :description => "my application",
       )
    end
    

    为了确信我的代码能够正常工作,我正在尝试 puts 条带错误(如果存在)。条带错误是我可以 看跌期权 ?

    例如:

    if Stripe.error.exists?
        puts Stripe.error
    end
    

    尝试使用查看输出 看跌期权 我的模型和测试文件中的命令如下:

    有意创建错误:

    StripeMock.prepare_card_error(:card_declined)
    

    我正在努力 看跌期权 这(但以下任何一项都不起作用):

    puts "Stripe Card Error: #{Stripe::CardError}"
    
    begin
      rescue Stripe::CardError => e
      body = e.json_body
      err  = body[:error]
      puts err.exists?
      puts err
    end
    
    puts err.exists?
    puts err
    

    当我尝试以下方式时:

    error = Stripe::CardError
    if error
      rescue Stripe::CardError => e
    end
    

    它无效,也无效 if Stripe.error if Stripe::CardError if Stripe::CardError.exists?

    一定有条带错误 object 我们可以应用逻辑吗?!?

    有关的任何帮助 看跌期权 请检查/检查条带错误,谢谢。

    0 回复  |  直到 7 年前
        1
  •  1
  •   hpar    7 年前

    基于对原始问题的评论:您希望捕获 Stripe::charge.create()

    begin
      puts "About to create charge"
      charge = Stripe::Charge.create(
        :amount => amount_in_cents,
        :customer => stripe_customer,
        :description => "my application",
      )
      # The next line only runs if Charge.create() did not raise an exception
    
      puts "Created charge #{charge.id}"
      MyDatabase.insert(charge.id)
    
    rescue Stripe::CardError => e
      # An error was thrown, so execution of the begin block did not complete
      puts e
    end
    

    这是 a brief tutorial on exception handling in Ruby