88import com .google .firebase .firestore .Source ;
99
1010import java .util .List ;
11- import java .util .concurrent .Callable ;
11+ import java .util .concurrent .ExecutionException ;
1212
1313import androidx .annotation .NonNull ;
1414import androidx .annotation .Nullable ;
1515import androidx .paging .PagingState ;
1616import androidx .paging .rxjava3 .RxPagingSource ;
1717import io .reactivex .rxjava3 .core .Single ;
18- import io .reactivex .rxjava3 .functions .Function ;
1918import io .reactivex .rxjava3 .schedulers .Schedulers ;
2019
2120public 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