From 08290d416813319f8f71d6726ee25a72b25129fa Mon Sep 17 00:00:00 2001 From: Annie Li Date: Wed, 24 May 2023 15:44:39 -0700 Subject: [PATCH 1/2] Handle developer message --- error.go | 1 + error_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/error.go b/error.go index 4f4dcd968e..8ad875fe70 100644 --- a/error.go +++ b/error.go @@ -232,6 +232,7 @@ type Error struct { HTTPStatusCode int `json:"status,omitempty"` Msg string `json:"message"` + DeveloperMsg string `json:"developer_message"` Param string `json:"param,omitempty"` PaymentIntent *PaymentIntent `json:"payment_intent,omitempty"` PaymentMethod *PaymentMethod `json:"payment_method,omitempty"` diff --git a/error_test.go b/error_test.go index 4adac46d1b..de80040247 100644 --- a/error_test.go +++ b/error_test.go @@ -42,6 +42,31 @@ func TestErrorResponse(t *testing.T) { assert.True(t, errors.As(err, &invalidRequestErr)) } +func TestPreviewErrorResponse(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Add("Request-Id", "req_123") + w.WriteHeader(http.StatusBadRequest) + fmt.Fprintln(w, `{"error":{"developer_message":"Unacceptable"}}`) + })) + defer ts.Close() + + backend := GetBackendWithConfig(APIBackend, &BackendConfig{ + // Suppress error log output to make a verbose run of this test less + // alarming (because we're testing specifically for an error). + LeveledLogger: &LeveledLogger{Level: LevelNull}, + + URL: String(ts.URL), + }) + + err := backend.Call(http.MethodGet, "/v1/charges/ch_123", "sk_test_123", nil, nil) + assert.Error(t, err) + + stripeErr := err.(*Error) + assert.Equal(t, "req_123", stripeErr.RequestID) + assert.Equal(t, 400, stripeErr.HTTPStatusCode) + assert.Equal(t, "Unacceptable", stripeErr.DeveloperMsg) +} + func TestErrorRedact(t *testing.T) { pi := &PaymentIntent{Amount: int64(400), ClientSecret: "foo"} si := &SetupIntent{Description: "keepme", ClientSecret: "foo"} From 26b1c395e6ff1202b6533bb8fe15c504c6d24017 Mon Sep 17 00:00:00 2001 From: Annie Li Date: Wed, 24 May 2023 15:51:22 -0700 Subject: [PATCH 2/2] omitempty --- error.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/error.go b/error.go index 8ad875fe70..ea78000b51 100644 --- a/error.go +++ b/error.go @@ -232,7 +232,7 @@ type Error struct { HTTPStatusCode int `json:"status,omitempty"` Msg string `json:"message"` - DeveloperMsg string `json:"developer_message"` + DeveloperMsg string `json:"developer_message,omitempty"` Param string `json:"param,omitempty"` PaymentIntent *PaymentIntent `json:"payment_intent,omitempty"` PaymentMethod *PaymentMethod `json:"payment_method,omitempty"`