From 5cca59f0fba583dd9bc80c2fe05732d6345b71b9 Mon Sep 17 00:00:00 2001 From: mramotar_dbx Date: Wed, 8 Nov 2023 14:43:44 -0500 Subject: [PATCH] First stab Signed-off-by: mramotar_dbx --- .../store/store5/FetcherResult.kt | 1 + .../store/store5/StoreReadResponse.kt | 23 +++++++++++++++++++ .../store/store5/impl/FetcherController.kt | 2 ++ 3 files changed, 26 insertions(+) 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..8f441493a 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 e71dd93a3..6d02bd386 100644 --- a/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/StoreReadResponse.kt +++ b/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/StoreReadResponse.kt @@ -58,6 +58,11 @@ sealed class StoreReadResponse { val message: String, override val origin: StoreReadResponseOrigin ) : Error() + + data class Custom( + val error: E, + override val origin: StoreReadResponseOrigin + ): Error() } /** @@ -101,6 +106,23 @@ sealed class StoreReadResponse { else -> null } + fun errorOrNull(): Throwable? { + if (this is Error.Exception) { + return error + } + + return 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 @@ -134,4 +156,5 @@ 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<*> -> throw 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..52bd6b24d 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,8 @@ internal class FetcherController -> StoreReadResponse.Error.Custom(it.error, StoreReadResponseOrigin.Fetcher()) } }.onEmpty { val origin =