Stay organized with collections
Save and categorize content based on your preferences.
public final class PagingSource.LoadResult.Error<Key extends Object, Value extends Object> extends PagingSource.LoadResult
Error result object for PagingSource.load.
This return type indicates an expected, recoverable error (such as a network load failure). This failure will be forwarded to the UI as a LoadState.Error, and may be retried.
import androidx.paging.PagingSource
import androidx.paging.PagingSource.LoadResult
import androidx.paging.PagingState
/**
* Sample page-keyed PagingSource, which uses Int page number to load pages.
*
* Loads Items from network requests via Retrofit to a backend service.
*
* Note that the key type is Int, since we're using page number to load a page.
*/
class MyPagingSource(val myBackend: MyBackendService) : PagingSource<Int, Item>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Item> {
// Retrofit calls that return the body type throw either IOException for network
// failures, or HttpException for any non-2xx HTTP status codes. This code reports all
// errors to the UI, but you can inspect/wrap the exceptions to provide more context.
return try {
// Key may be null during a refresh, if no explicit key is passed into Pager
// construction. Use 0 as default, because our API is indexed started at index 0
val pageNumber = params.key ?: 0
// Suspending network load via Retrofit. This doesn't need to be wrapped in a
// withContext(Dispatcher.IO) { ... } block since Retrofit's Coroutine
// CallAdapter dispatches on a worker thread.
val response = myBackend.searchItems(pageNumber)
// Since 0 is the lowest page number, return null to signify no more pages should
// be loaded before it.
val prevKey = if (pageNumber > 0) pageNumber - 1 else null
// This API defines that it's out of data when a page returns empty. When out of
// data, we return `null` to signify no more pages should be loaded
val nextKey = if (response.items.isNotEmpty()) pageNumber + 1 else null
LoadResult.Page(data = response.items, prevKey = prevKey, nextKey = nextKey)
} catch (e: IOException) {
LoadResult.Error(e)
} catch (e: HttpException) {
LoadResult.Error(e)
}
}
override fun getRefreshKey(state: PagingState<Int, Item>): Int? {
return state.anchorPosition?.let {
state.closestPageToPosition(it)?.prevKey?.plus(1)
?: state.closestPageToPosition(it)?.nextKey?.minus(1)
}
}
}
Summary
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-07-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-07-10 UTC."],[],[],null,["# PagingSource.LoadResult.Error\n=============================\n\nArtifact: [androidx.paging:paging-common](/jetpack/androidx/releases/paging) \n[View Source](https://cs.android.com/search?q=file:androidx/paging/PagingSource.kt+class:androidx.paging.PagingSource.LoadResult.Error) \nAdded in [3.0.0](/jetpack/androidx/releases/paging#3.0.0)\n\n*** ** * ** ***\n\n[Kotlin](/reference/kotlin/androidx/paging/PagingSource.LoadResult.Error \"View this page in Kotlin\") \\|Java\n\n\n```\npublic final class PagingSource.LoadResult.Error\u003cKey extends Object, Value extends Object\u003e extends PagingSource.LoadResult\n```\n\n\u003cbr /\u003e\n\n|---|---|-----------------------------------------------------------------------------------------------------------|\n| [java.lang.Object](https://developer.android.com/reference/java/lang/Object.html) |||\n| ↳ | [androidx.paging.PagingSource.LoadResult](/reference/androidx/paging/PagingSource.LoadResult) ||\n| | ↳ | [androidx.paging.PagingSource.LoadResult.Error](/reference/androidx/paging/PagingSource.LoadResult.Error) |\n\n*** ** * ** ***\n\nError result object for [PagingSource.load](/reference/androidx/paging/PagingSource#load(androidx.paging.PagingSource.LoadParams)).\n\nThis return type indicates an expected, recoverable error (such as a network load failure). This failure will be forwarded to the UI as a [LoadState.Error](/reference/androidx/paging/LoadState.Error), and may be retried. \n\n```kotlin\nimport androidx.paging.PagingSource\nimport androidx.paging.PagingSource.LoadResult\nimport androidx.paging.PagingState\n\n/**\n * Sample page-keyed PagingSource, which uses Int page number to load pages.\n *\n * Loads Items from network requests via Retrofit to a backend service.\n *\n * Note that the key type is Int, since we're using page number to load a page.\n */\nclass MyPagingSource(val myBackend: MyBackendService) : PagingSource\u003cInt, Item\u003e() {\n override suspend fun load(params: LoadParams\u003cInt\u003e): LoadResult\u003cInt, Item\u003e {\n\n // Retrofit calls that return the body type throw either IOException for network\n // failures, or HttpException for any non-2xx HTTP status codes. This code reports all\n // errors to the UI, but you can inspect/wrap the exceptions to provide more context.\n return try {\n // Key may be null during a refresh, if no explicit key is passed into Pager\n // construction. Use 0 as default, because our API is indexed started at index 0\n val pageNumber = params.key ?: 0\n\n // Suspending network load via Retrofit. This doesn't need to be wrapped in a\n // withContext(Dispatcher.IO) { ... } block since Retrofit's Coroutine\n // CallAdapter dispatches on a worker thread.\n val response = myBackend.searchItems(pageNumber)\n\n // Since 0 is the lowest page number, return null to signify no more pages should\n // be loaded before it.\n val prevKey = if (pageNumber \u003e 0) pageNumber - 1 else null\n\n // This API defines that it's out of data when a page returns empty. When out of\n // data, we return `null` to signify no more pages should be loaded\n val nextKey = if (response.items.isNotEmpty()) pageNumber + 1 else null\n LoadResult.Page(data = response.items, prevKey = prevKey, nextKey = nextKey)\n } catch (e: IOException) {\n LoadResult.Error(e)\n } catch (e: HttpException) {\n LoadResult.Error(e)\n }\n }\n\n override fun getRefreshKey(state: PagingState\u003cInt, Item\u003e): Int? {\n return state.anchorPosition?.let {\n state.closestPageToPosition(it)?.prevKey?.plus(1)\n ?: state.closestPageToPosition(it)?.nextKey?.minus(1)\n }\n }\n}\n```\n\nSummary\n-------\n\n| ### Public constructors |\n|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `\u003cKey extends `[Object](https://developer.android.com/reference/java/lang/Object.html)`, Value extends `[Object](https://developer.android.com/reference/java/lang/Object.html)`\u003e `[Error](/reference/androidx/paging/PagingSource.LoadResult.Error#Error(kotlin.Throwable))`(@`[NonNull](/reference/androidx/annotation/NonNull)` `[Throwable](https://developer.android.com/reference/java/lang/Throwable.html)` throwable)` |\n\n| ### Public methods |\n|--------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------|\n| `final @`[NonNull](/reference/androidx/annotation/NonNull)` `[Throwable](https://developer.android.com/reference/java/lang/Throwable.html) | [getThrowable](/reference/androidx/paging/PagingSource.LoadResult.Error#getThrowable())`()` |\n| `@`[NonNull](/reference/androidx/annotation/NonNull)` `[String](https://developer.android.com/reference/java/lang/String.html) | [toString](/reference/androidx/paging/PagingSource.LoadResult.Error#toString())`()` |\n\nPublic constructors\n-------------------\n\n### Error\n\n```\npublic \u003cKey extends Object, Value extends Object\u003e Error(@NonNull Throwable throwable)\n``` \n\nPublic methods\n--------------\n\n### getThrowable\n\nAdded in [3.0.0](/jetpack/androidx/releases/paging#3.0.0) \n\n```\npublic final @NonNull Throwable getThrowable()\n``` \n\n### toString\n\n```\npublic @NonNull String toString()\n```"]]