【问题标题】:Create Error object for Stripe::Error (Ruby on rails)为 Stripe::Error 创建 Error 对象(Ruby on rails)
【发布时间】:2019-07-16 15:08:56
【问题描述】:

我能够创建如下标准错误...

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))。

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

【问题讨论】:

    标签: ruby-on-rails ruby error-handling stripe-payments standard-error


    【解决方案1】:

    您可以在此处找到 Stripe 错误的最新方法定义: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 参数。 一般来说,这似乎代表一个条带资源。

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

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

    希望有帮助!

    【讨论】:

    • 您可以通过提出某种无效请求来提高它,例如retrievedelete 一个不存在的对象。你也可以通过写raise Stripe::InvalidRequestError.new('No such plan: test_plan', 'plan')来强行提出它
    • 如何手动提升?没有提出无效请求?
    • 刚刚在您发表最后一条评论后编辑了我的帖子 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-18
    • 2016-06-06
    相关资源
    最近更新 更多