Skip to content

Commit

Permalink
Add StripeError#idempotent_replayed? (#907)
Browse files Browse the repository at this point in the history
Adds an easy accessor on `StripeError` which indicates whether the error
occurred previously, but was replayed on this request because the user
passed the same idempotency key as that original request.

Fixes #905.
  • Loading branch information
brandur committed Feb 27, 2020
1 parent de40bbf commit f7923f7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/stripe/errors.rb
Expand Up @@ -25,6 +25,7 @@ def initialize(message = nil, http_status: nil, http_body: nil,
@http_status = http_status
@http_body = http_body
@http_headers = http_headers || {}
@idempotent_replayed = @http_headers["idempotent-replayed"] == "true"
@json_body = json_body
@code = code
@request_id = @http_headers["request-id"]
Expand All @@ -37,6 +38,13 @@ def construct_error_object
ErrorObject.construct_from(@json_body[:error])
end

# Whether the error was the result of an idempotent replay, meaning that it
# originally occurred on a previous request and is being replayed back
# because the user sent the same idempotency key for this one.
def idempotent_replayed?
@idempotent_replayed
end

def to_s
status_string = @http_status.nil? ? "" : "(Status #{@http_status}) "
id_string = @request_id.nil? ? "" : "(Request #{@request_id}) "
Expand Down
12 changes: 12 additions & 0 deletions test/stripe/errors_test.rb
Expand Up @@ -14,6 +14,18 @@ class StripeErrorTest < Test::Unit::TestCase
end
end

context "#idempotent_replayed?" do
should "initialize from header" do
e = StripeError.new("message", http_headers: { "idempotent-replayed" => "true" })
assert_equal true, e.idempotent_replayed?
end

should "be 'falsey' by default" do
e = StripeError.new("message")
refute_equal true, e.idempotent_replayed?
end
end

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

0 comments on commit f7923f7

Please sign in to comment.