Skip to content

Commit

Permalink
Handle developer message in preview error responses (#1663)
Browse files Browse the repository at this point in the history
* Handle developer message

* omitempty
  • Loading branch information
anniel-stripe authored May 24, 2023
1 parent 1de1537 commit e7a5224
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ type Error struct {

HTTPStatusCode int `json:"status,omitempty"`
Msg string `json:"message"`
DeveloperMsg string `json:"developer_message,omitempty"`
Param string `json:"param,omitempty"`
PaymentIntent *PaymentIntent `json:"payment_intent,omitempty"`
PaymentMethod *PaymentMethod `json:"payment_method,omitempty"`
Expand Down
25 changes: 25 additions & 0 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down

0 comments on commit e7a5224

Please sign in to comment.