# PagingSource.LoadResult.Error

Artifact: [androidx.paging:paging-common](https://developer.android.com/jetpack/androidx/releases/paging) [View Source](https://cs.android.com/search?q=file:androidx/paging/PagingSource.kt+class:androidx.paging.PagingSource.LoadResult.Error) Added in [3.0.0](https://developer.android.com/jetpack/androidx/releases/paging#3.0.0)

*** ** * ** ***


```
public final class PagingSource.LoadResult.Error<Key extends Object, Value extends Object> extends PagingSource.LoadResult<@NonNull Key, @NonNull Value>
```

<br />

|---|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object.html) |||
| ↳ | [androidx.paging.PagingSource.LoadResult](https://developer.android.com/reference/androidx/paging/PagingSource.LoadResult)\<@[NonNull](https://developer.android.com/reference/androidx/annotation/NonNull) Key, @[NonNull](https://developer.android.com/reference/androidx/annotation/NonNull) Value\> ||
|   | ↳ | [androidx.paging.PagingSource.LoadResult.Error](https://developer.android.com/reference/androidx/paging/PagingSource.LoadResult.Error) |

*** ** * ** ***

Error result object for `https://developer.android.com/reference/androidx/paging/PagingSource#load(androidx.paging.PagingSource.LoadParams)`.

This return type indicates an expected, recoverable error (such as a network load failure). This failure will be forwarded to the UI as a `https://developer.android.com/reference/androidx/paging/LoadState.Error`, and may be retried.

```kotlin
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

| ### Public constructors |
|---|
| `<Key extends https://developer.android.com/reference/java/lang/Object.html, Value extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/paging/PagingSource.LoadResult.Error#Error(kotlin.Throwable)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Throwable.html throwable)` |

| ### Public methods |
|---|---|
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Throwable.html` | `https://developer.android.com/reference/androidx/paging/PagingSource.LoadResult.Error#getThrowable()()` |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/paging/PagingSource.LoadResult.Error#toString()()` |

## Public constructors

### Error

```
public <Key extends Object, Value extends Object> Error(@NonNull Throwable throwable)
```

## Public methods

### getThrowable

Added in [3.0.0](https://developer.android.com/jetpack/androidx/releases/paging#3.0.0)

```
public final @NonNull Throwable getThrowable()
```

### toString

```
public @NonNull String toString()
```