From 5b6be75a5435c7ab08dc568d111ef22aa8971c4c Mon Sep 17 00:00:00 2001 From: mramotar_dbx Date: Fri, 10 Nov 2023 12:20:07 -0500 Subject: [PATCH] Open up custom error generic Signed-off-by: mramotar_dbx --- .../store/store5/FetcherResult.kt | 2 +- .../store/store5/StoreReadResponse.kt | 14 ++++++++++---- .../store/store5/impl/FetcherController.kt | 5 ++++- 3 files changed, 15 insertions(+), 6 deletions(-) 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 8f441493a..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,6 +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() + 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 6d02bd386..aff87c614 100644 --- a/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/StoreReadResponse.kt +++ b/store/src/commonMain/kotlin/org/mobilenativefoundation/store/store5/StoreReadResponse.kt @@ -59,10 +59,10 @@ sealed class StoreReadResponse { override val origin: StoreReadResponseOrigin ) : Error() - data class Custom( + data class Custom( val error: E, override val origin: StoreReadResponseOrigin - ): Error() + ) : Error() } /** @@ -115,7 +115,7 @@ sealed class StoreReadResponse { } @Suppress("UNCHECKED_CAST") - fun errorOrNull(): E? { + fun errorOrNull(): E? { if (this is Error.Custom<*>) { return (this as? Error.Custom)?.error } @@ -156,5 +156,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<*> -> throw error + 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 52bd6b24d..cc772e6e2 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 @@ -93,7 +93,10 @@ internal class FetcherController -> StoreReadResponse.Error.Custom(it.error, StoreReadResponseOrigin.Fetcher()) + is FetcherResult.Error.Custom<*> -> StoreReadResponse.Error.Custom( + it.error, + StoreReadResponseOrigin.Fetcher() + ) } }.onEmpty { val origin =