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

Raise an error when requests params are invalid #874

Merged

Conversation

joeltaylor
Copy link
Contributor

@joeltaylor joeltaylor commented Oct 25, 2019

There are two kinds of API operations: collection and element specific.
The signature between the two is slightly different:

  • collection: (params, opts)
  • element specific: (id, params, opts)

If a user doesn't realize the difference, they may attempt to use the
collection signature when performing an element specific operation like:

Stripe::PaymentIntent.cancel('pi_1234', 'sk_test_key')
 # Results in an error: NoMethodError: undefined method `key?' for "sk_test"

The resulting error message isn't very useful for debugging.

Instead,this PR adds a message letting the user know what it's expecting:
request params should be either a Hash or nil (was a String)

Addresses #855

@joeltaylor joeltaylor force-pushed the add-error-message-for-invalid-params branch from 0509573 to b7f32e6 Compare October 25, 2019 21:31
@joeltaylor
Copy link
Contributor Author

I played around with a few different ideas to make a helpful error message. Originally, I wanted to incorporate something about the method signature in the error, but I couldn't find a good universal approach.

@brandur-stripe
Copy link
Contributor

@ob-stripe It looks like Joel and yourself were already discussing this in #855, so if it's okay, I'm going to leave review to you. For what it's worth, seems fine to me — can't go wrong with better error messages.

@@ -47,6 +48,14 @@ def request(method, url, params = {}, opts = {})
end
end

private def error_on_invalid_params(params)
return if params.is_a?(Hash)
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this check for nil too, since we accept it as a valid value?

Suggested change
return if params.is_a?(Hash)
return if params.nil? || params.is_a?(Hash)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Whoops, yeah there should be a nilcheck there. I'll add a spec for that, too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ob-stripe Actually, it looks like passing nil breaks warn_on_opts_in_params since it assumes params will respond to #key?.

I can add something like params ||= {} before the error/warn checks?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, that sounds reasonable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool, just pushed up the addition with a regression spec.

There are two kinds of API operations: collection and element specific.
The signature between the two is slightly different:
  - **collection**: (params, opts)
  - **element specific**: (id, params, opts)

If a user doesn't realize the difference, they may attempt to use the
collection signature when performing an element specific operation like:
```
Stripe::PaymentIntent.cancel('pi_1234', 'sk_test_key')
 # Results in an error: NoMethodError: undefined method `key?' for "sk_test"
```

The resulting error message isn't very useful for debugging.

Instead,this PR adds a message letting the user know what it's expecting:
`request params should be either a Hash or nil (was a String)`
@joeltaylor joeltaylor force-pushed the add-error-message-for-invalid-params branch from b7f32e6 to e889646 Compare October 30, 2019 02:56
@joeltaylor joeltaylor requested a review from ob-stripe October 31, 2019 16:21
Copy link
Contributor

@ob-stripe ob-stripe left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks @joeltaylor

@ob-stripe ob-stripe merged commit 299e9ea into stripe:master Oct 31, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants