Skip to content

Commit 7643930

Browse files
authored
refactor(rtdb, firestore): prevent paging from throwing ExecutionException (#1988)
1 parent c46730d commit 7643930

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

database/src/main/java/com/firebase/ui/database/paging/DatabasePagingSource.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,13 @@ public Single<LoadResult<String, DataSnapshot>> loadSingle(@NonNull LoadParams<S
8888
details).toException();
8989
}
9090
} catch (ExecutionException e) {
91-
throw new Exception(e.getCause());
91+
if (e.getCause() instanceof Exception) {
92+
// throw the original Exception
93+
throw (Exception) e.getCause();
94+
}
95+
// Only throw a new Exception when the original
96+
// Throwable cannot be cast to Exception
97+
throw new Exception(e);
9298
}
9399
}).subscribeOn(Schedulers.io()).onErrorReturn(LoadResult.Error::new);
94100
}

firestore/src/main/java/com/firebase/ui/firestore/paging/FirestorePagingSource.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@
88
import com.google.firebase.firestore.Source;
99

1010
import java.util.List;
11-
import java.util.concurrent.Callable;
11+
import java.util.concurrent.ExecutionException;
1212

1313
import androidx.annotation.NonNull;
1414
import androidx.annotation.Nullable;
1515
import androidx.paging.PagingState;
1616
import androidx.paging.rxjava3.RxPagingSource;
1717
import io.reactivex.rxjava3.core.Single;
18-
import io.reactivex.rxjava3.functions.Function;
1918
import io.reactivex.rxjava3.schedulers.Schedulers;
2019

2120
public class FirestorePagingSource extends RxPagingSource<PageKey, DocumentSnapshot> {
@@ -39,18 +38,24 @@ public Single<LoadResult<PageKey, DocumentSnapshot>> loadSingle(@NonNull LoadPar
3938
}
4039

4140
return Single.fromCallable(() -> {
42-
Tasks.await(task);
43-
if (task.isSuccessful()) {
41+
try {
42+
Tasks.await(task);
4443
QuerySnapshot snapshot = task.getResult();
4544
PageKey nextPage = getNextPageKey(snapshot);
4645
if (snapshot.getDocuments().isEmpty()) {
4746
return toLoadResult(snapshot.getDocuments(), null);
4847
}
4948
return toLoadResult(snapshot.getDocuments(), nextPage);
49+
} catch (ExecutionException e) {
50+
if (e.getCause() instanceof Exception) {
51+
// throw the original Exception
52+
throw (Exception) e.getCause();
53+
}
54+
// Only throw a new Exception when the original
55+
// Throwable cannot be cast to Exception
56+
throw new Exception(e);
5057
}
51-
throw task.getException();
52-
}).subscribeOn(Schedulers.io())
53-
.onErrorReturn(throwable -> new LoadResult.Error<>(throwable));
58+
}).subscribeOn(Schedulers.io()).onErrorReturn(LoadResult.Error::new);
5459
}
5560

5661
private LoadResult<PageKey, DocumentSnapshot> toLoadResult(

0 commit comments

Comments
 (0)