为 Stripe::Error(rails 上的 Ruby)创建错误对象

Create Error object for Stripe::Error (Ruby on rails)

我可以创建如下标准错误...

StandardError.new("No such customer: invalid-id")

但是,我想知道如何创建特定错误,特别是 Stripe 错误...

https://stripe.com/docs/api/errors/handling https://github.com/stripe/stripe-ruby/blob/382ae0b45d848304f7c1739696f33458c86bee4f/lib/stripe/errors.rb#L99

Stripe::RateLimitError
Stripe::InvalidRequestError
Stripe::AuthenticationError
Stripe::InvalidRequestError
Stripe::StripeError

产生这些错误的最佳方法是什么?我用它来传递给我的模拟 Api 库。我发现了这个...

https://github.com/stripe/stripe-ruby/blob/master/test/stripe/errors_test.rb

我试过了... Stripe::InvalidRequestError.new('this is a test') ,但我得到一个 ArgumentError(参数数量错误(给定 1,预期 2))。

所需的第二个参数是什么?

您可以在此处找到 Stripe Errors 的最新方法定义:https://github.com/stripe/stripe-ruby/blob/ec91de6849f34d8d6701a6e91a1b2ee0d50c21ea/lib/stripe/errors.rb

这是Stripe::InvalidRequestError

的方法定义
class InvalidRequestError < StripeError
  attr_accessor :param

  def initialize(message, param, http_status: nil, http_body: nil,
                 json_body: nil, http_headers: nil, code: nil)
    super(message, http_status: http_status, http_body: http_body,
                   json_body: json_body, http_headers: http_headers,
                   code: code)
    @param = param
  end
end

因此,在回答您的问题时,第二个参数是 param 参数。 一般来说,这似乎代表一个条带资源。

例如,如果我想在尝试执行与 Stripe::Plan class 相关的操作时创建一个 InvalidRequestError,我将使用以下代码:

Stripe::InvalidRequestError.new('No such plan: test_plan', 'plan')

希望对您有所帮助!