-
Notifications
You must be signed in to change notification settings - Fork 33
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
base: master
Are you sure you want to change the base?
Conversation
richarddd
commented
Feb 28, 2017
- Adding json exception handling allows for subscribers to know if json parsing has failed
- 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>() { |
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.
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")); |
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 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) { |
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.
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?
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.
True, however, when we have a JSONException we want to know that the JSON is invalid
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.
Two things:
-
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. -
Again, not suggesting removing the logging for
JSONException
s, rather saying log any exception.
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.
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) { |
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.
same as above
return PurchaseType.SUBSCRIPTION; | ||
} else { | ||
throw new IllegalArgumentException("Unknown purchase type: " + value); | ||
switch (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.
This can be dangerous if value
isn't marked @NotNull
. Users will get an NPE if it is.
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.
+1
Thanks for the PR @richarddd. |
@lukaspili @rharter updated! |