无法在成功时返回结果。

huangapple go评论80阅读模式
英文:

Unable to return Result on success

问题

以下是您要的代码部分的中文翻译:

我有以下函数

inline fun <reified T> deserialize(payload: SdkBytes): Result<T> {
    return runCatching {
        Json.decodeFromString(payload.asUtf8String())
    }
}

我正在调用一个 AWS Lambda 函数该函数检查是否发生了 functionError并基于此来反序列化 payload

private fun InvokeResponse.Response(): Result<Response> {
    return runCatching {
        val error = this.functionError()
        if (error.isNullOrBlank()) {
            deserialize<Response>(this.payload())
        }
        throw RuntimeException(error)
    }
}

我将其用于返回响应代码如下

response.statusCode = lambdaResponse
            .Response()
            .fold(::onSucess, ::onFailure)
return response

然而无论如何它都进入 onFailure我希望它在能够反序列化后进入 OnSuccess但似乎在 deserialize 后我没有得到一个 `Result`
英文:

I have a function as below:

 inline fun <reified T> deserialize(payload: SdkBytes): Result<T> {
        return runCatching {
            Json.decodeFromString(payload.asUtf8String())
        }
    }

I am invoking an AWS Lambda function which checks wether a functionError has occured or not and based on that deserializes the payload:

private fun InvokeResponse.Response(): Result<Response> {
        return runCatching {
            val error = this.functionError()
            if (error.isNullOrBlank()) {
                deserialize<Response>(this.payload())
            }
            throw RuntimeException(error)
        }
    }

and I am using it to return responseCodes as below

response.statusCode = lambdaResponse
            .Response()
            .fold(::onSucess, ::onFailure)
        return response

However for all the cases it goes to onFailure. What I want is that it should go to OnSucess if it is able to deserialize but seems like I am not getting a Result after deserialize

答案1

得分: 1

首先,我想指出,runCatchingResult 可能不应该在任何这些情况下使用:https://stackoverflow.com/questions/70847513/when-and-how-to-use-result-in-kotlin/70847760#70847760

现在,看一下 Response(),你总是抛出一个 RuntimeException,因为你的 throwif 之后,但不在 else 子句中:

if (error.isNullOrBlank()) {
    deserialize<Response>(this.payload())
}
throw RuntimeException(error) // 即使没有错误,这也会运行

你可能想要:

if (error.isNullOrBlank()) {
    deserialize<Response>(this.payload())
} else {
    throw RuntimeException(error)
}

话虽如此,你可以完全删除 runCatching,而是使用自己的结果类型(不需要抛出),或者在更高层次捕获异常。

*顺便说一下,考虑在 Response() 中使用小写的 r,以符合 Kotlin 函数的约定。

英文:

First I'd like to point out that runCatching and Result should probably not be used in any of those cases: https://stackoverflow.com/questions/70847513/when-and-how-to-use-result-in-kotlin/70847760#70847760

Now, looking at Response()*, you are always throwing a RuntimeException because your throw is after the if, but not in an else clause:

if (error.isNullOrBlank()) {
    deserialize&lt;Response&gt;(this.payload())
}
throw RuntimeException(error) // this runs even when there is no error

You probably wanted:

if (error.isNullOrBlank()) {
    deserialize&lt;Response&gt;(this.payload())
} else {
    throw RuntimeException(error)
}

That said, you could instead remove runCatching altogether and either use your own result type (no need for throw) or just catch exceptions at a higher level.

*By the way, consider a lowercase R in Response() to follow Kotlin conventions for functions.

huangapple
  • 本文由 发表于 2023年3月7日 21:37:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75662700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定