Project Wide Utilities: Creating error handling wrapper - devrath/RunTracer GitHub Wiki
Error.kt
interface Error
Result.kt
sealed interface Result<out D, out E: Error> {
data class Success<out D>(val data: D): Result<D, Nothing>
data class Error<out E: com.istudio.core.domain.util.Error>(val error: E): Result<Nothing, E>
}
inline fun <T, E: Error, R> Result<T, E>.map(map: (T) -> R): Result<R, E> {
return when(this) {
is Result.Error -> Result.Error(error)
is Result.Success -> Result.Success(map(data))
}
}
fun <T, E: Error> Result<T, E>.asEmptyDataResult(): EmptyDataResult<E> {
return map { }
}
typealias EmptyDataResult<E> = Result<Unit, E>
DataError.kt
sealed interface DataError: Error {
enum class Network: DataError {
REQUEST_TIMEOUT,
UNAUTHORIZED,
CONFLICT,
TOO_MANY_REQUESTS,
NO_INTERNET,
PAYLOAD_TOO_LARGE,
SERVER_ERROR,
SERIALIZATION,
UNKNOWN
}
enum class Local: DataError {
DISK_FULL
}
}