Skip to content

Commit

Permalink
Handle developer message in preview error responses (#1224)
Browse files Browse the repository at this point in the history
  • Loading branch information
pakrym-stripe authored May 23, 2023
1 parent a77bbab commit 99dbffb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ group :development do
# up-to-date, but it's not the end of the world if it's not.
gem "rubocop", "0.80"

# jaro_winkler 1.5.5 installation fails for jruby
gem "jaro_winkler", "1.5.4"

platforms :mri do
gem "byebug"
gem "pry"
Expand Down
17 changes: 9 additions & 8 deletions lib/stripe/stripe_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -768,10 +768,11 @@ def self.maybe_gc_connection_managers
end

private def specific_api_error(resp, error_data, context)
message = error_data[:message] || error_data[:developer_message]
Util.log_error("Stripe API error",
status: resp.http_status,
error_code: error_data[:code],
error_message: error_data[:message],
error_message: message,
error_param: error_data[:param],
error_type: error_data[:type],
idempotency_key: context.idempotency_key,
Expand All @@ -792,26 +793,26 @@ def self.maybe_gc_connection_managers
when 400, 404
case error_data[:type]
when "idempotency_error"
IdempotencyError.new(error_data[:message], **opts)
IdempotencyError.new(message, **opts)
else
InvalidRequestError.new(
error_data[:message], error_data[:param],
message, error_data[:param],
**opts
)
end
when 401
AuthenticationError.new(error_data[:message], **opts)
AuthenticationError.new(message, **opts)
when 402
CardError.new(
error_data[:message], error_data[:param],
message, error_data[:param],
**opts
)
when 403
PermissionError.new(error_data[:message], **opts)
PermissionError.new(message, **opts)
when 429
RateLimitError.new(error_data[:message], **opts)
RateLimitError.new(message, **opts)
else
APIError.new(error_data[:message], **opts)
APIError.new(message, **opts)
end
end

Expand Down
11 changes: 11 additions & 0 deletions test/stripe/preview_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,16 @@ class PreviewTest < Test::Unit::TestCase
assert_equal "application/json", req.headers["Content-Type"]
assert_equal stripe_context, req.headers["Stripe-Context"]
end

should "include developer_message in error message" do
expected_body = "{\"error\": {\"developer_message\": \"Unacceptable!\"}}"
stub_request(:get, "#{Stripe.api_base}/v2/accounts/acc_123")
.to_return(body: expected_body, status: 400)

e = assert_raises do
Stripe::Preview.get("/v2/accounts/acc_123")
end
assert_equal "Unacceptable!", e.message
end
end
end

0 comments on commit 99dbffb

Please sign in to comment.