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

Json exception handling #16

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Conversation

richarddd
Copy link

  1. Adding json exception handling allows for subscribers to know if json parsing has failed
  2. Adding a check if service is bound allows subscribers to be notified if we are unable to bind, i.e billing is not supported

throw error if service fails to bind so it could be handleled by subscriber
added json exception to log method
purchase = PurchaseParser.parse(data.getStringExtra("INAPP_PURCHASE_DATA"));
} catch (final JSONException e) {
ReactiveBilling.log(e, "Cannot parse purchase json");
observable.mergeWith(Observable.create(new Observable.OnSubscribe<PurchaseResponse>() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this be simplified to observable.mergeWith(Observable.error(e))?

} catch (SecurityException e) {
ReactiveBilling.log(e, "Bind service error");
subscriber.onError(e);
}

//service is not bound, throw exception
if(!bound){
subscriber.onError(new IllegalStateException("Service unable to bind"));
Copy link
Contributor

Choose a reason for hiding this comment

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

If we can't bind to the service, do we need to exit here as opposed to continuing below?

@@ -33,7 +36,10 @@ protected void onBillingServiceReady(BillingService billingService, Observer<? s
try {
observer.onNext(billingService.getPurchases(purchaseType, continuationToken));
observer.onCompleted();
} catch (RemoteException e) {
} catch (RemoteException | JSONException e) {
if(e instanceof JSONException) {
Copy link
Contributor

Choose a reason for hiding this comment

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

IMHO catch clauses should only be collapsed if they're identical. If you have to instanceof check in here, they should be separate clauses. That being said, is there a reason not to log all errors here?

Copy link
Author

Choose a reason for hiding this comment

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

True, however, when we have a JSONException we want to know that the JSON is invalid

Copy link
Contributor

Choose a reason for hiding this comment

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

Two things:

  1. I'm not suggesting getting rid of the log message, I'm suggesting that if there are different actions in the case of different exception types, then having multiple catch blocks is clearer than collapsing them into one so they can be separated by instanceof checks.

  2. Again, not suggesting removing the logging for JSONExceptions, rather saying log any exception.

Copy link
Author

Choose a reason for hiding this comment

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

Valid point! 👍

@@ -31,7 +35,10 @@ protected void onBillingServiceReady(BillingService billingService, Observer<? s
try {
observer.onNext(billingService.getSkuDetails(purchaseType, productIds));
observer.onCompleted();
} catch (RemoteException e) {
} catch (RemoteException | JSONException e) {
if(e instanceof JSONException) {
Copy link
Contributor

Choose a reason for hiding this comment

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

same as above

return PurchaseType.SUBSCRIPTION;
} else {
throw new IllegalArgumentException("Unknown purchase type: " + value);
switch (value) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This can be dangerous if value isn't marked @NotNull. Users will get an NPE if it is.

Copy link
Author

Choose a reason for hiding this comment

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

+1

@lukaspili
Copy link
Owner

Thanks for the PR @richarddd.
Can you integrate the changes suggested by @rharter before merging?

@richarddd
Copy link
Author

richarddd commented Apr 11, 2017

@lukaspili @rharter updated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants