Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add StripeError#idempotent_replayed? #907

Merged
merged 1 commit into from Feb 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"
ob-stripe marked this conversation as resolved.
Show resolved Hide resolved
@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?
ob-stripe marked this conversation as resolved.
Show resolved Hide resolved
@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