-
Notifications
You must be signed in to change notification settings - Fork 658
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
Conversation
@@ -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"); |
There was a problem hiding this comment.
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()
?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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()
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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"
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see above
There was a problem hiding this comment.
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"); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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" + |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 " + |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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)
r? @mshafrir-stripe thanks for the review! |
} catch (InvocationTargetException e) { | ||
throw new IllegalArgumentException("Exception instantiating " + ephemeralKeyClass, e); | ||
if (e.getTargetException() != null){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
space before {
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
onKeyUpdate
was called outside atry
clause which is not ideal