Returning a Kotlin result in a coroutine context throws an exception

Viewed 1365

I have a simple setting:

lifecycleScope.launch {
    val result = test()
}
    
suspend fun test(): Result<Unit> = withContext(Dispatchers.IO)  {
    Result.failure(IllegalStateException("QWER"))
}

Actual result: The code above is crashing with: java.lang.IllegalStateException: QWER

Expected result: Result object returned as expected

Im using kotlin 1.4.10 and coroutines 1.3.9. Im using a kotlin.Result object as a return type, for that I have:

kotlinOptions {
    freeCompilerArgs = ["-Xallow-result-return-type"]
}

Another thing is if I'm executing the coroutine with the main context(Dispatchers.Main) everything works as expected .

1 Answers

UPDATE From Kotlin 1.5, Result be used as a direct result type of Kotlin functions. more details in KEEP

I have the same crash. the codes work when Kotlin is 1.3.72 but crash at 1.4.0. my solution is copying the kotlin.Result to my project.

class Result<out T>(val value: Any?) {

    val isSuccess: Boolean get() = value !is Failure

    val isFailure: Boolean get() = value is Failure

    fun getOrNull(): T? = when {
        isFailure -> null
        else -> value as T
    }

    fun exceptionOrNull(): Throwable? = when (value) {
        is Failure -> value.exception
        else -> null
    }

    override fun toString(): String =
        when (value) {
            is Failure -> value.toString() // "Failure($exception)"
            else -> "Success($value)"
        }

    companion object {
        fun <T> success(value: T): Result<T> = Result(value)

        fun <T> failure(exception: Throwable): Result<T> = Result(createFailure(exception))
    }

    class Failure(val exception: Throwable) {
        override fun equals(other: Any?): Boolean = other is Failure && exception == other.exception
        override fun hashCode(): Int = exception.hashCode()
        override fun toString(): String = "Failure($exception)"
    }
}

private fun createFailure(exception: Throwable): Any = Result.Failure(exception)

inline fun <R, T> Result<T>.fold(
    onSuccess: (value: T) -> R,
    onFailure: (exception: Throwable) -> R
): R {
    return when (val exception = exceptionOrNull()) {
        null -> onSuccess(value as T)
        else -> onFailure(exception)
    }
}

inline fun <R, T> Result<T>.map(transform: (value: T) -> R): Result<R> {
    return when {
        isSuccess -> Result.success(transform(value as T))
        else -> Result(value)
    }
}

inline fun <T> Result<T>.onFailure(action: (exception: Throwable) -> Unit): Result<T> {
    exceptionOrNull()?.let { action(it) }
    return this
}

inline fun <T> Result<T>.onSuccess(action: (value: T) -> Unit): Result<T> {
    if (isSuccess) action(value as T)
    return this
}

fun <T> Result<T>.getOrThrow(): T {
    throwOnFailure()
    return value as T
}

fun Result<*>.throwOnFailure() {
    if (value is Result.Failure) throw value.exception
}

inline fun <R, T : R> Result<T>.getOrElse(onFailure: (exception: Throwable) -> R): R {
    return when (val exception = exceptionOrNull()) {
        null -> value as T
        else -> onFailure(exception)
    }
}

fun <R, T : R> Result<T>.getOrDefault(defaultValue: R): R {
    if (isFailure) return defaultValue
    return value as T
}
Related