Add support for code attribute on all Stripe exceptions

This commit is contained in:
Olivier Bellone 2018-02-23 14:18:51 +01:00
parent a7ea9cf1e9
commit 3805968741
No known key found for this signature in database
GPG Key ID: 11E77E3AA0C40303
4 changed files with 28 additions and 22 deletions

View File

@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2017-10-30 14:39:50 +0100 using RuboCop version 0.50.0.
# on 2018-02-23 14:17:07 +0100 using RuboCop version 0.50.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
@ -8,23 +8,23 @@
# Offense count: 19
Metrics/AbcSize:
Max: 49
Max: 45
# Offense count: 27
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/BlockLength:
Max: 467
Max: 469
# Offense count: 8
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 595
Max: 597
# Offense count: 10
# Offense count: 11
Metrics/CyclomaticComplexity:
Max: 13
# Offense count: 257
# Offense count: 259
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
@ -33,14 +33,14 @@ Metrics/LineLength:
# Offense count: 32
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 47
Max: 45
# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ModuleLength:
Max: 305
# Offense count: 5
# Offense count: 6
# Configuration parameters: CountKeywordArgs.
Metrics/ParameterLists:
Max: 7
@ -55,6 +55,6 @@ Style/ClassVars:
- 'lib/stripe/stripe_object.rb'
- 'test/stripe/api_resource_test.rb'
# Offense count: 52
# Offense count: 53
Style/Documentation:
Enabled: false

View File

@ -8,8 +8,7 @@ module Stripe
# about the response that conveyed the error.
attr_accessor :response
# These fields are now available as part of #response and that usage should
# be preferred.
attr_reader :code
attr_reader :http_body
attr_reader :http_headers
attr_reader :http_status
@ -18,12 +17,13 @@ module Stripe
# Initializes a StripeError.
def initialize(message = nil, http_status: nil, http_body: nil, json_body: nil,
http_headers: nil)
http_headers: nil, code: nil)
@message = message
@http_status = http_status
@http_body = http_body
@http_headers = http_headers || {}
@json_body = json_body
@code = code
@request_id = @http_headers[:request_id]
end
@ -55,14 +55,15 @@ module Stripe
# CardError is raised when a user enters a card that can't be charged for
# some reason.
class CardError < StripeError
attr_reader :param, :code
attr_reader :param
# TODO: make code a keyword arg in next major release
def initialize(message, param, code, http_status: nil, http_body: nil, json_body: nil,
http_headers: nil)
super(message, http_status: http_status, http_body: http_body,
json_body: json_body, http_headers: http_headers)
json_body: json_body, http_headers: http_headers,
code: code)
@param = param
@code = code
end
end
@ -77,9 +78,10 @@ module Stripe
attr_accessor :param
def initialize(message, param, http_status: nil, http_body: nil, json_body: nil,
http_headers: nil)
http_headers: nil, code: nil)
super(message, http_status: http_status, http_body: http_body,
json_body: json_body, http_headers: http_headers)
json_body: json_body, http_headers: http_headers,
code: code)
@param = param
end
end
@ -109,13 +111,11 @@ module Stripe
module OAuth
# OAuthError is raised when the OAuth API returns an error.
class OAuthError < StripeError
attr_accessor :code
def initialize(code, description, http_status: nil, http_body: nil, json_body: nil,
http_headers: nil)
super(description, http_status: http_status, http_body: http_body,
json_body: json_body, http_headers: http_headers)
@code = code
json_body: json_body, http_headers: http_headers,
code: code)
end
end

View File

@ -294,6 +294,7 @@ module Stripe
http_headers: resp.http_headers,
http_status: resp.http_status,
json_body: resp.data,
code: error_data[:code],
}
case resp.http_status
@ -310,6 +311,9 @@ module Stripe
when 401
AuthenticationError.new(error_data[:message], opts)
when 402
# TODO: modify CardError constructor to make code a keyword argument
# so we don't have to delete it from opts
opts.delete(:code)
CardError.new(
error_data[:message], error_data[:param], error_data[:code],
opts

View File

@ -452,13 +452,15 @@ module Stripe
should "raise CardError on 402" do
stub_request(:post, "#{Stripe.api_base}/v1/charges")
.to_return(body: JSON.generate(make_missing_id_error), status: 402)
.to_return(body: JSON.generate(make_invalid_exp_year_error), status: 402)
client = StripeClient.new
begin
client.execute_request(:post, "/v1/charges")
rescue Stripe::CardError => e
assert_equal(402, e.http_status)
assert_equal(true, e.json_body.is_a?(Hash))
assert_equal("invalid_expiry_year", e.code)
assert_equal("exp_year", e.param)
end
end