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

Ruby on rails 如何检查条带错误是否存在/如果存在,则放置条带错误,ruby-on-rails,stripe-payments,Ruby On Rails,Stripe Payments,在我的Rails应用程序中,我只尝试在不存在条带错误的情况下运行条带充电命令。我希望能够检查条带错误 charge = Stripe::Charge.create( :amount => amount_in_cents, :customer => stripe_customer, :description => "my application", ) 应该是这样的: unless Stripe.errors.exists? do charge = Strip

在我的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
error = Stripe::CardError
if error
  rescue Stripe::CardError => e
end
为了确信我的代码正在工作,我正在尝试
放入条带错误(如果存在)。条带错误是我可以放入的对象吗

例如:

if Stripe.error.exists?
    puts Stripe.error
end
尝试使用
查看输出会将
命令放入我的模型以及我的测试文件中,如下所示:

要故意创建错误,请执行以下操作:

StripeMock.prepare_card_error(:card_declined)
我试图
置于此位置(但以下各项均无效):

当我尝试类似于:

unless Stripe.errors.exists? do
   charge = Stripe::Charge.create(
      :amount => amount_in_cents,
      :customer => stripe_customer,
      :description => "my application",
   )
end
error = Stripe::CardError
if error
  rescue Stripe::CardError => e
end
它无效,如果Stripe.error存在,
如果Stripe::carderor
如果Stripe::carderor.exists,则它也无效。

一定有一些条带错误
对象
,我们可以对其应用逻辑


如果您有任何关于
put
ing/inspection条带错误的帮助,我们将不胜感激,谢谢。

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

开始
放入“即将创建费用”
charge=Stripe::charge.create(
:amount=>金额(单位:美分),
:customer=>stripe\u customer,
:description=>“我的应用程序”,
)
#下一行仅在Charge.create()未引发异常时运行
放置“已创建费用#{charge.id}”
MyDatabase.insert(charge.id)
救援条带::CardError=>e
#引发错误,因此begin块的执行未完成
放e
结束

以下是。

是否要查看条带错误的
堆栈跟踪
?@mehedi我希望仅当存在错误时才能运行代码。类似于:
if Stripe::carderro
-->
rescue Stripe::carderro=>e
-->
end
但这是无效的,如果Stripe::carderro
if Stripe::carderro.existed?
。我希望能够检查错误。你应该尝试在你的
范围内进行充电。。。拯救。。。结束
阻塞并使用
rescue
部件上的各种错误类别。下面是一个关于我们如何使用示例应用程序完成此操作的示例,希望对您有所帮助。谢谢。另外,在我们的两个代码示例中,我一直在寻找的
对象实际上是“
e
”。