Skip to content

Commit

Permalink
Merge 6f8a5dd into 8b45b1d
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed Aug 16, 2019
2 parents 8b45b1d + 6f8a5dd commit 8fd3b8d
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 8 deletions.
1 change: 1 addition & 0 deletions lib/stripe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
require "stripe/stripe_object"
require "stripe/stripe_response"
require "stripe/list_object"
require "stripe/error_object"
require "stripe/api_resource"
require "stripe/singleton_api_resource"
require "stripe/webhook"
Expand Down
61 changes: 61 additions & 0 deletions lib/stripe/error_object.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# frozen_string_literal: true

module Stripe
class ErrorObject < StripeObject
# Unlike other objects, we explicitly declare getter methods here. This
# is because the API doesn't return `null` values for fields on this
# object, rather the fields are omitted entirely. Not declaring the getter
# methods would cause users to run into `NoMethodError` exceptions and
# prevent generic error handling.

def charge
@values[:charge]
end

def code
@values[:code]
end

def doc_url
@values[:doc_url]
end

def message
@values[:message]
end

def param
@values[:param]
end

def payment_intent
@values[:payment_intent]
end

def payment_method
@values[:payment_method]
end

def setup_intent
@values[:setup_intent]
end

def source
@values[:source]
end

def type
@values[:type]
end

# For OAuth errors

def oauth_error
@values[:error]
end

def oauth_error_description
@values[:error_description]
end
end
end
12 changes: 12 additions & 0 deletions lib/stripe/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class StripeError < StandardError
attr_accessor :response

attr_reader :code
attr_reader :error
attr_reader :http_body
attr_reader :http_headers
attr_reader :http_status
Expand All @@ -27,6 +28,12 @@ def initialize(message = nil, http_status: nil, http_body: nil,
@json_body = json_body
@code = code
@request_id = @http_headers[:request_id]
@error = construct_error_object
end

def construct_error_object
return nil if @json_body.nil? || !@json_body.key?(:error)
ErrorObject.construct_from(@json_body[:error])
end

def to_s
Expand Down Expand Up @@ -119,6 +126,11 @@ def initialize(code, description, http_status: nil, http_body: nil,
json_body: json_body, http_headers: http_headers,
code: code)
end

def construct_error_object
return nil if @json_body.nil?
ErrorObject.construct_from(@json_body)
end
end

# InvalidClientError is raised when the client doesn't belong to you, or
Expand Down
37 changes: 29 additions & 8 deletions test/stripe/errors_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,37 @@

module Stripe
class StripeErrorTest < Test::Unit::TestCase
context "#to_s" do
should "convert to string" do
e = StripeError.new("message")
assert_equal "message", e.to_s
context "StripeError" do
context "#initialize" do
should "initialize error if json_body is set" do
e = StripeError.new("message", json_body: { error: { code: "some_error" } })
assert_not_nil e.error
assert_equal "some_error", e.error.code
assert_nil e.error.charge
end
end

context "#to_s" do
should "convert to string" do
e = StripeError.new("message")
assert_equal "message", e.to_s

e = StripeError.new("message", http_status: 200)
assert_equal "(Status 200) message", e.to_s

e = StripeError.new("message", http_status: 200)
assert_equal "(Status 200) message", e.to_s
e = StripeError.new("message", http_status: nil, http_body: nil, json_body: nil, http_headers: { request_id: "request-id" })
assert_equal "(Request request-id) message", e.to_s
end
end
end

e = StripeError.new("message", http_status: nil, http_body: nil, json_body: nil, http_headers: { request_id: "request-id" })
assert_equal "(Request request-id) message", e.to_s
context "OAuth::OAuthError" do
context "#initialize" do
should "initialize error if json_body is set" do
e = OAuth::OAuthError.new("message", "description", json_body: { error: "some_oauth_error" })
assert_not_nil e.error
assert_equal "some_oauth_error", e.error.error
end
end
end
end
Expand Down

0 comments on commit 8fd3b8d

Please sign in to comment.