Skip to content

Commit ca844e8

Browse files
authored
make api error fields generated (#1901)
1 parent 44721d5 commit ca844e8

3 files changed

Lines changed: 50 additions & 19 deletions

File tree

.rubocop.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Layout/LineLength:
1818
Exclude:
1919
- "lib/stripe/object_types.rb"
2020
- "lib/stripe/stripe_client.rb"
21+
- "lib/stripe/error_object.rb"
2122
- "lib/stripe/resources/**/*.rb"
2223
- "lib/stripe/services/**/*.rb"
2324
- "lib/stripe/events/**/*.rb"

lib/stripe/error_object.rb

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,69 +11,93 @@ class ErrorObject < StripeObject
1111
# methods would cause users to run into `NoMethodError` exceptions and
1212
# get in the way of generic error handling.
1313

14+
# errorFields: The beginning of the section generated from our OpenAPI spec
15+
# For card errors resulting from a card issuer decline, a short string indicating [how to proceed with an error](https://docs.stripe.com/declines#retrying-issuer-declines) if they provide one.
16+
def advice_code
17+
@values[:advice_code]
18+
end
19+
1420
# For card errors, the ID of the failed charge.
1521
def charge
1622
@values[:charge]
1723
end
1824

19-
# For some errors that could be handled programmatically, a short string
20-
# indicating the error code reported.
25+
# For some errors that could be handled programmatically, a short string indicating the [error code](https://docs.stripe.com/error-codes) reported.
2126
def code
2227
@values[:code]
2328
end
2429

25-
# For card errors resulting from a card issuer decline, a short string
26-
# indicating the card issuer's reason for the decline if they provide one.
30+
# For card errors resulting from a card issuer decline, a short string indicating the [card issuer's reason for the decline](https://docs.stripe.com/declines#issuer-declines) if they provide one.
2731
def decline_code
2832
@values[:decline_code]
2933
end
3034

31-
# A URL to more information about the error code reported.
35+
# A URL to more information about the [error code](https://docs.stripe.com/error-codes) reported.
3236
def doc_url
3337
@values[:doc_url]
3438
end
3539

36-
# A human-readable message providing more details about the error. For card
37-
# errors, these messages can be shown to your users.
40+
# A human-readable message providing more details about the error. For card errors, these messages can be shown to your users.
3841
def message
3942
@values[:message]
4043
end
4144

42-
# If the error is parameter-specific, the parameter related to the error.
43-
# For example, you can use this to display a message near the correct form
44-
# field.
45+
# For card errors resulting from a card issuer decline, a 2 digit code which indicates the advice given to merchant by the card network on how to proceed with an error.
46+
def network_advice_code
47+
@values[:network_advice_code]
48+
end
49+
50+
# For payments declined by the network, an alphanumeric code which indicates the reason the payment failed.
51+
def network_decline_code
52+
@values[:network_decline_code]
53+
end
54+
55+
# If the error is parameter-specific, the parameter related to the error. For example, you can use this to display a message near the correct form field.
4556
def param
4657
@values[:param]
4758
end
4859

49-
# The PaymentIntent object for errors returned on a request involving a
50-
# PaymentIntent.
60+
# The PaymentIntent object for errors returned on a request involving a PaymentIntent.
5161
def payment_intent
5262
@values[:payment_intent]
5363
end
5464

55-
# The PaymentMethod object for errors returned on a request involving a
56-
# PaymentMethod.
65+
# The PaymentMethod object for errors returned on a request involving a PaymentMethod.
5766
def payment_method
5867
@values[:payment_method]
5968
end
6069

61-
# The SetupIntent object for errors returned on a request involving a
62-
# SetupIntent.
70+
# If the error is specific to the type of payment method, the payment method type that had a problem. This field is only populated for invoice-related errors.
71+
def payment_method_type
72+
@values[:payment_method_type]
73+
end
74+
75+
# A URL to the request log entry in your dashboard.
76+
def request_log_url
77+
@values[:request_log_url]
78+
end
79+
80+
# The SetupIntent object for errors returned on a request involving a SetupIntent.
6381
def setup_intent
6482
@values[:setup_intent]
6583
end
6684

67-
# The source object for errors returned on a request involving a source.
85+
# The PaymentSource object for errors returned on a request involving a PaymentSource.
6886
def source
6987
@values[:source]
7088
end
7189

72-
# The type of error returned. One of `api_error`, `card_error`,
73-
# `idempotency_error`, or `invalid_request_error`.
90+
# The type of error returned. One of `api_error`, `card_error`, `idempotency_error`, or `invalid_request_error`
7491
def type
7592
@values[:type]
7693
end
94+
95+
# The user message associated with the error.
96+
def user_message
97+
@values[:user_message]
98+
end
99+
100+
# errorFields: The end of the section generated from our OpenAPI spec
77101
end
78102

79103
# Represents on OAuth error returned by the OAuth API.

test/stripe/errors_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ class StripeErrorTest < Test::Unit::TestCase
1414
end
1515
end
1616

17+
should "expose network_advice_code from error body" do
18+
e = StripeError.new("message", json_body: { error: { type: "card_error", network_advice_code: "02" } })
19+
assert_not_nil e.error
20+
assert_equal "02", e.error.network_advice_code
21+
end
22+
1723
context "#idempotent_replayed?" do
1824
should "initialize from header" do
1925
e = StripeError.new("message", http_headers: { "idempotent-replayed" => "true" })

0 commit comments

Comments
 (0)