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

Fix bug calling onKeyUpdate when should not, and better error messages #780

Merged
merged 8 commits into from
Jan 24, 2019

Conversation

acavailhez-stripe
Copy link
Contributor

  • onKeyUpdate was called outside a try clause which is not ideal
  • The error messages where not incredibly useful, made them a tad better

@@ -216,7 +216,7 @@ String getType() {
protected static <TEphemeralKey extends AbstractEphemeralKey> TEphemeralKey
fromString(@Nullable String rawJson, Class ephemeralKeyClass) throws JSONException {
if (rawJson == null) {
return null;
throw new IllegalArgumentException("Exception instantiating " + ephemeralKeyClass+ " null raw key");
Copy link
Collaborator

Choose a reason for hiding this comment

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

How about "Attempted to instantiate " + ephemeralKeyClass + " with null key JSON"? Can you also add a test? Does it need to be ephemeralKeyClass.getSimpleName()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

getSimpleName makes the name a tad shorter, since the error message is quite long.

I'll change the text

A test is not trivial because of the was TestEphemeralKeyProvider is build, but I'll re-try

Copy link
Contributor Author

Choose a reason for hiding this comment

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

all done by the way

null,
CustomerEphemeralKey.class);

verify(mKeyManagerListener, times(0)).onKeyUpdate(
Copy link
Collaborator

Choose a reason for hiding this comment

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

you can do never() instead of times(0)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

public void triggerCorrectErrorOnInvalidRawKey() {

mTestEphemeralKeyProvider.setNextRawEphemeralKey("Not_a_JSON");
EphemeralKeyManager<CustomerEphemeralKey> keyManager = new EphemeralKeyManager(
Copy link
Collaborator

Choose a reason for hiding this comment

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

new EphemeralKeyManager<>(...)

CustomerEphemeralKey.class);

verify(mKeyManagerListener, times(0)).onKeyUpdate(
(CustomerEphemeralKey) isNull(), (String) isNull(), (Map<String, Object>) isNull());
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this is better:

any(CustomerEphemeralKey.class), anyString(), ArgumentMatchers.<String, Object>anyMap()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So I initially tried that, but it fails to error when onKeyUpdate is called with nulls

Copy link
Collaborator

Choose a reason for hiding this comment

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

don't we expect it not to be called though? so I would assume "never with any" makes more logical sense than "never with nulls"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yes, but if I change to the any and re-introduce the bug where onKeyUpdate is called, the test still passes

null,
CustomerEphemeralKey.class);

verify(mKeyManagerListener, times(0)).onKeyUpdate(
Copy link
Collaborator

Choose a reason for hiding this comment

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

see above

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

if (key == null) {
mListener.onKeyError(HttpURLConnection.HTTP_INTERNAL_ERROR,
"EphemeralKeyUpdateListener.onKeyUpdate was called " +
"with a null value");
Copy link
Collaborator

Choose a reason for hiding this comment

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

should we return; after this line?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah yes, good catch

+ e.getLocalizedMessage());
"EphemeralKeyUpdateListener.onKeyUpdate was passed " +
"a value that could not be JSON parsed: ["
+ e.getLocalizedMessage() + "] the raw body from Stripe's response" +
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think "the raw body..." should be a new sentence, so "]. The raw body...

Also below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes ok

} catch (InvocationTargetException e) {
throw new IllegalArgumentException("Exception instantiating " + ephemeralKeyClass, e);
if (e.getTargetException() != null){
throw new IllegalArgumentException("Improperly formatted JSON for ephemeral key " +
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we can remove the e.getTargetException().getMessage() part from the message because we're passing e.getTargetException() as an argument.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hm, in an ideal world yes, but the interface of onKeyError only accepts a String, and that's what we're passing to the dev.

So without explicitely dropping the message in the String, we risk losing it as it is surfaced, if that makes sense

"a value that could not be JSON parsed: ["
+ e.getLocalizedMessage() + "] the raw body from Stripe's response" +
" should be passed");
} catch (Exception e) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

did you add this for safety?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, if any other exception happens, the error will just be silenced, which was the case for a wrongly formatted JSON (the JSON is parsed, but then the CustomerEphemeralKey cannot be instanciated)

@acavailhez-stripe
Copy link
Contributor Author

r? @mshafrir-stripe thanks for the review!
Still 3 open discussions

} catch (InvocationTargetException e) {
throw new IllegalArgumentException("Exception instantiating " + ephemeralKeyClass, e);
if (e.getTargetException() != null){
Copy link
Collaborator

Choose a reason for hiding this comment

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

space before {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

try {
mEphemeralKey = AbstractEphemeralKey.fromString(key, mEphemeralKeyClass);
mListener.onKeyUpdate(mEphemeralKey, actionString, arguments);
} catch (JSONException e) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

do we still need to explicitly catch JSONException if we have the catch all below?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it helps the message be a tad more explicit

@acavailhez-stripe acavailhez-stripe merged commit ac547fc into master Jan 24, 2019
@acavailhez-stripe acavailhez-stripe deleted the acavailhez/ephemeral-key-fix branch January 24, 2019 00:12
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.

3 participants