Skip to content

Commit bbf1d9e

Browse files
author
Joe Hansche
committed
Try not to swallow exceptions while subscribing
If the query can't be encoded (or any other checked or unchecked Exception occurs), the async SubscribeClientOperation will swallow the exception, leaving no way (even a log message) to see why it failed. This creates a new LiveQueryException subclass (UnknownException) to represent an unexpected RuntimeException that can then be handled by the HandleErrorCallback#onError() callback.
1 parent 316eb7e commit bbf1d9e

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

ParseLiveQuery/src/main/java/com/parse/LiveQueryException.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,23 @@ private LiveQueryException(String detailMessage) {
1010
super(detailMessage);
1111
}
1212

13+
private LiveQueryException(String detailMessage, Throwable cause) {
14+
super(detailMessage, cause);
15+
}
16+
17+
private LiveQueryException(Throwable cause) {
18+
super(cause);
19+
}
20+
21+
/**
22+
* An error that is reported when any other unknown {@link RuntimeException} occurs unexpectedly.
23+
*/
24+
public static class UnknownException extends LiveQueryException {
25+
/* package */ UnknownException(String detailMessage, RuntimeException cause) {
26+
super(detailMessage, cause);
27+
}
28+
}
29+
1330
/**
1431
* An error that is reported when the server returns a response that cannot be parsed.
1532
*/

ParseLiveQuery/src/main/java/com/parse/ParseLiveQueryClientImpl.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,21 @@ private Subscription<T> subscriptionForRequestId(int requestId) {
223223
return subscriptions.get(requestId);
224224
}
225225

226-
private void sendSubscription(Subscription<T> subscription) {
227-
sendOperationAsync(new SubscribeClientOperation<>(subscription.getRequestId(), subscription.getQueryState()));
226+
private void sendSubscription(final Subscription<T> subscription) {
227+
SubscribeClientOperation<T> op = new SubscribeClientOperation<>(subscription.getRequestId(), subscription.getQueryState());
228+
// dispatch errors
229+
sendOperationAsync(op).continueWith(new Continuation<Void, Void>() {
230+
public Void then(Task<Void> task) {
231+
Exception error = task.getError();
232+
if (error != null) {
233+
if (error instanceof RuntimeException) {
234+
subscription.didEncounter(new LiveQueryException.UnknownException(
235+
"Error when subscribing", (RuntimeException) error), subscription.getQuery());
236+
}
237+
}
238+
return null;
239+
}
240+
});
228241
}
229242

230243
private void sendUnsubscription(Subscription subscription) {

0 commit comments

Comments
 (0)