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

Error early when params not specified for card functions #484

Merged
merged 2 commits into from
Oct 24, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions card/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func New(params *stripe.CardParams) (*stripe.Card, error) {
}

func (c Client) New(params *stripe.CardParams) (*stripe.Card, error) {
if params == nil {
return nil, errors.New("params should not be nil")
}

body := &form.Values{}

// Note that we call this special append method instead of the standard one
Expand Down Expand Up @@ -71,6 +75,10 @@ func Get(id string, params *stripe.CardParams) (*stripe.Card, error) {
}

func (c Client) Get(id string, params *stripe.CardParams) (*stripe.Card, error) {
if params == nil {
return nil, errors.New("params should not be nil")
}

var body *form.Values
var commonParams *stripe.Params

Expand Down Expand Up @@ -103,6 +111,10 @@ func Update(id string, params *stripe.CardParams) (*stripe.Card, error) {
}

func (c Client) Update(id string, params *stripe.CardParams) (*stripe.Card, error) {
if params == nil {
return nil, errors.New("params should not be nil")
}

body := &form.Values{}
form.AppendTo(body, params)

Expand All @@ -129,15 +141,16 @@ func Del(id string, params *stripe.CardParams) (*stripe.Card, error) {
}

func (c Client) Del(id string, params *stripe.CardParams) (*stripe.Card, error) {
if params == nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're already doing if params != nil { below (not visible) so it's redundant. Should you fix the one below in that case?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah good point. Unfortunately the other one would still result in a panic because of the check on params.Account immediately afterwards, so I've taken that one out in favor of my addition.

return nil, errors.New("params should not be nil")
}

var body *form.Values
var commonParams *stripe.Params

if params != nil {
body = &form.Values{}

form.AppendTo(body, params)
commonParams = &params.Params
}
body = &form.Values{}
form.AppendTo(body, params)
commonParams = &params.Params

card := &stripe.Card{}
var err error
Expand Down Expand Up @@ -166,14 +179,20 @@ func (c Client) List(params *stripe.CardListParams) *Iter {
var lp *stripe.ListParams
var p *stripe.Params

form.AppendTo(body, params)
lp = &params.ListParams
p = params.ToParams()
if params != nil {
form.AppendTo(body, params)
lp = &params.ListParams
p = params.ToParams()
}

return &Iter{stripe.GetIter(lp, body, func(b *form.Values) ([]interface{}, stripe.ListMeta, error) {
list := &stripe.CardList{}
var err error

if params == nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just error above like you do in the other functions if params == nil?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, basically because List functions only return an *Iter (and not an error). The first opportunity we get to return an error is the first time the iterator iterates, and so the check on params is further down than you'd expect.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah gotcha, that makes sense, thanks for clarifying!

return nil, list.ListMeta, errors.New("params should not be nil")
}

if len(params.Account) > 0 {
err = c.B.Call("GET", fmt.Sprintf("/accounts/%v/external_accounts", params.Account), c.Key, b, p, list)
} else if len(params.Customer) > 0 {
Expand Down
28 changes: 27 additions & 1 deletion card/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ func TestCardDel(t *testing.T) {
assert.NotNil(t, card)
}

func TestCardDel_RequiresParams(t *testing.T) {
_, err := Del("card_123", nil)
assert.Error(t, err, "params should not be nil")
}

func TestCardGet(t *testing.T) {
card, err := Get("card_123", &stripe.CardParams{
Customer: "cus_123",
Expand All @@ -24,7 +29,12 @@ func TestCardGet(t *testing.T) {
assert.NotNil(t, card)
}

func TestCardListByCustomer(t *testing.T) {
func TestCardGet_RequiresParams(t *testing.T) {
_, err := Get("card_123", nil)
assert.Error(t, err, "params should not be nil")
}

func TestCardList_ByCustomer(t *testing.T) {
i := List(&stripe.CardListParams{Customer: "cus_123"})

// Verify that we can get at least one card
Expand All @@ -33,6 +43,12 @@ func TestCardListByCustomer(t *testing.T) {
assert.NotNil(t, i.Card())
}

func TestCardList_RequiresParams(t *testing.T) {
i := List(nil)
assert.False(t, i.Next())
assert.Error(t, i.Err(), "params should not be nil")
}

func TestCardNew(t *testing.T) {
card, err := New(&stripe.CardParams{
Customer: "cus_123",
Expand All @@ -42,6 +58,11 @@ func TestCardNew(t *testing.T) {
assert.NotNil(t, card)
}

func TestCardNew_RequiresParams(t *testing.T) {
_, err := New(nil)
assert.Error(t, err, "params should not be nil")
}

func TestCardUpdate(t *testing.T) {
card, err := Update("card_123", &stripe.CardParams{
Customer: "cus_123",
Expand All @@ -50,3 +71,8 @@ func TestCardUpdate(t *testing.T) {
assert.Nil(t, err)
assert.NotNil(t, card)
}

func TestCardUpdate_RequiresParams(t *testing.T) {
_, err := Update("card_123", nil)
assert.Error(t, err, "params should not be nil")
}