在协程上下文中返回Kotlin结果会抛出异常。

5

我有一个简单的设置:

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

实际结果: 上述代码出现了崩溃: java.lang.IllegalStateException: QWER

预期结果: 成功返回结果对象

我正在使用Kotlin 1.4.10和Coroutines 1.3.9,使用Kotlin.Result对象作为返回类型,代码如下:

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

另外一件事是,如果我使用主线程上下文(Dispatchers.Main)执行协程,一切都按预期工作。

1个回答

4

更新:从Kotlin 1.5开始,Result可以作为Kotlin函数的直接返回类型。更多详情请参考KEEP文档

我也遇到了同样的崩溃问题。在使用Kotlin 1.3.72时代码可以正常运行,但在1.4.0版本会崩溃。我的解决方法是将kotlin.Result复制到我的项目中。

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
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接