diff --git a/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/FetcherResult.kt b/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/FetcherResult.kt index 9e9d63e19..9b146c594 100644 --- a/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/FetcherResult.kt +++ b/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/FetcherResult.kt @@ -5,5 +5,6 @@ sealed class FetcherResult { sealed class Error : FetcherResult() { data class Exception(val error: Throwable) : Error() data class Message(val message: String) : Error() + data class Custom(val error: E) : Error() } } diff --git a/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/StoreReadResponse.kt b/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/StoreReadResponse.kt index 658baa216..97f0531a1 100644 --- a/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/StoreReadResponse.kt +++ b/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/StoreReadResponse.kt @@ -44,8 +44,8 @@ sealed class StoreReadResponse { StoreReadResponse() /** - * No new data event dispatched by Store to signal the [Fetcher] returned no data (i.e the - * returned [kotlinx.coroutines.Flow], when collected, was empty). + * No new data event dispatched by Store to signal the [Fetcher] returned no data (i.e., the + * returned [kotlinx.coroutines.flow.Flow], when collected, was empty). */ data class NoNewData(override val origin: StoreReadResponseOrigin) : StoreReadResponse() @@ -62,6 +62,11 @@ sealed class StoreReadResponse { val message: String, override val origin: StoreReadResponseOrigin ) : Error() + + data class Custom( + val error: E, + override val origin: StoreReadResponseOrigin + ) : Error() } /** @@ -105,6 +110,26 @@ sealed class StoreReadResponse { else -> null } + private fun errorOrNull(): Throwable? { + if (this is Error.Exception) { + return error + } + + return null + } + + /** + * @returns Error if there is one, else null. + */ + @Suppress("UNCHECKED_CAST") + fun errorOrNull(): E? { + if (this is Error.Custom<*>) { + return (this as? Error.Custom)?.error + } + + return errorOrNull() as? E + } + @Suppress("UNCHECKED_CAST") internal fun swapType(): StoreReadResponse = when (this) { is Error -> this @@ -141,4 +166,11 @@ sealed class StoreReadResponseOrigin { fun StoreReadResponse.Error.doThrow(): Nothing = when (this) { is StoreReadResponse.Error.Exception -> throw error is StoreReadResponse.Error.Message -> throw RuntimeException(message) + is StoreReadResponse.Error.Custom<*> -> { + if (error is Throwable) { + throw error + } else { + throw RuntimeException("Non-throwable custom error: $error") + } + } } diff --git a/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/FetcherController.kt b/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/FetcherController.kt index 4ed04fa62..1e5a4138d 100644 --- a/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/FetcherController.kt +++ b/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/impl/FetcherController.kt @@ -92,6 +92,10 @@ internal class FetcherController -> StoreReadResponse.Error.Custom( + it.error, + StoreReadResponseOrigin.Fetcher() + ) } }.onEmpty { val origin =