英文:
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
首先,我想指出,runCatching
和 Result
可能不应该在任何这些情况下使用:https://stackoverflow.com/questions/70847513/when-and-how-to-use-result-in-kotlin/70847760#70847760
现在,看一下 Response()
,你总是抛出一个 RuntimeException
,因为你的 throw
在 if
之后,但不在 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<Response>(this.payload())
}
throw RuntimeException(error) // this runs even when there is no error
You probably wanted:
if (error.isNullOrBlank()) {
deserialize<Response>(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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论