Skip to content

Commit

Permalink
First stab
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 8, 2023
1 parent 3559a78 commit 5cca59f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ sealed class StoreReadResponse<out Output> {
val message: String,
override val origin: StoreReadResponseOrigin
) : Error()

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

/**
Expand Down Expand Up @@ -101,6 +106,23 @@ sealed class StoreReadResponse<out Output> {
else -> null
}

fun errorOrNull(): Throwable? {
if (this is Error.Exception) {
return error
}

return null
}

@Suppress("UNCHECKED_CAST")
fun <E: Throwable> errorOrNull(): E? {
if (this is Error.Custom<*>) {
return (this as? Error.Custom<E>)?.error
}

return errorOrNull() as? E
}

@Suppress("UNCHECKED_CAST")
internal fun <T> swapType(): StoreReadResponse<T> = when (this) {
is Error -> this
Expand Down Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ internal class FetcherController<Key : Any, Network : Any, Output : Any, Local :
it.error,
origin = StoreReadResponseOrigin.Fetcher()
)

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

0 comments on commit 5cca59f

Please sign in to comment.