Skip to content

Commit

Permalink
Add support for code attribute on all Stripe exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed Feb 23, 2018
1 parent a7ea9cf commit 3805968
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 22 deletions.
18 changes: 9 additions & 9 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
# 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
# versions of RuboCop, may require this file to be generated again.

# 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:
Expand All @@ -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
Expand All @@ -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
24 changes: 12 additions & 12 deletions lib/stripe/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ class StripeError < StandardError
# 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
Expand All @@ -18,12 +17,13 @@ class StripeError < StandardError

# 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

Expand Down Expand Up @@ -55,14 +55,15 @@ class APIError < StripeError
# 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

Expand All @@ -77,9 +78,10 @@ class InvalidRequestError < StripeError
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
Expand Down Expand Up @@ -109,13 +111,11 @@ def initialize(message, sig_header, http_body: nil)
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

Expand Down
4 changes: 4 additions & 0 deletions lib/stripe/stripe_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ def specific_api_error(resp, error_data, context)
http_headers: resp.http_headers,
http_status: resp.http_status,
json_body: resp.data,
code: error_data[:code],
}

case resp.http_status
Expand All @@ -310,6 +311,9 @@ def specific_api_error(resp, error_data, context)
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
Expand Down
4 changes: 3 additions & 1 deletion test/stripe/stripe_client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -452,13 +452,15 @@ class StripeClientTest < Test::Unit::TestCase

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

Expand Down

0 comments on commit 3805968

Please sign in to comment.