1 /* 2 * Copyright 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package androidx.paging.testing 18 19 import androidx.annotation.VisibleForTesting 20 import androidx.paging.CombinedLoadStates 21 import androidx.paging.LoadState 22 import androidx.paging.PagingDataPresenter 23 import androidx.paging.PagingSource 24 import androidx.paging.PagingSource.LoadResult 25 26 /** 27 * An interface to implement the error recovery strategy when [PagingSource] returns a 28 * [LoadResult.Error]. 29 */ 30 @VisibleForTesting 31 public fun interface LoadErrorHandler { 32 /** 33 * The lambda that should return an [ErrorRecovery] given the [CombinedLoadStates] indicating 34 * which [LoadState] contains the [LoadState.Error]. 35 * 36 * Sample use case: val onError = LoadErrorHandler { combinedLoadStates -> if 37 * (combinedLoadStates.refresh is LoadResult.Error) { ErrorRecovery.RETRY } else { 38 * ErrorRecovery.THROW } } 39 */ onErrornull40 public fun onError(combinedLoadStates: CombinedLoadStates): ErrorRecovery 41 } 42 43 /** 44 * The method of recovery when [PagingSource] returns [LoadResult.Error]. The error is indicated 45 * when [PagingDataPresenter.loadStateFlow] emits a [CombinedLoadStates] where one or more of the 46 * [LoadState] is [LoadState.Error]. 47 */ 48 @VisibleForTesting 49 public enum class ErrorRecovery { 50 /** 51 * Rethrow the original [Throwable][LoadState.Error.error] that was caught when loading from the 52 * data source. 53 */ 54 THROW, 55 56 /** 57 * Retry the failed load. This does not guarantee a successful load as the data source may still 58 * return an error. 59 */ 60 RETRY, 61 62 /** Returns a snapshot with any data that has been loaded up till the point of error. */ 63 RETURN_CURRENT_SNAPSHOT, 64 } 65