Skip to content

Commit

Permalink
Open up custom error generic
Browse files Browse the repository at this point in the history
Signed-off-by: mramotar_dbx <[email protected]>
  • Loading branch information
matt-ramotar committed Nov 10, 2023
1 parent 5cca59f commit 5b6be75
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ sealed class FetcherResult<out Network : Any> {
sealed class Error : FetcherResult<Nothing>() {
data class Exception(val error: Throwable) : Error()
data class Message(val message: String) : Error()
data class Custom<E: Throwable>(val error: E): Error()
data class Custom<E : Any>(val error: E) : Error()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ sealed class StoreReadResponse<out Output> {
override val origin: StoreReadResponseOrigin
) : Error()

data class Custom<E: Throwable>(
data class Custom<E : Any>(
val error: E,
override val origin: StoreReadResponseOrigin
): Error()
) : Error()
}

/**
Expand Down Expand Up @@ -115,7 +115,7 @@ sealed class StoreReadResponse<out Output> {
}

@Suppress("UNCHECKED_CAST")
fun <E: Throwable> errorOrNull(): E? {
fun <E : Any> errorOrNull(): E? {
if (this is Error.Custom<*>) {
return (this as? Error.Custom<E>)?.error
}
Expand Down Expand Up @@ -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")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ internal class FetcherController<Key : Any, Network : Any, Output : Any, Local :
origin = StoreReadResponseOrigin.Fetcher()
)

is FetcherResult.Error.Custom<*> -> StoreReadResponse.Error.Custom(it.error, StoreReadResponseOrigin.Fetcher())
is FetcherResult.Error.Custom<*> -> StoreReadResponse.Error.Custom(
it.error,
StoreReadResponseOrigin.Fetcher()
)
}
}.onEmpty {
val origin =
Expand Down

0 comments on commit 5b6be75

Please sign in to comment.