I am trying to use Kotlin runCatching feature along with response + error translation functions. Something like
class SomeClassThatCallsServiceX {
fun remoteCall(input): TransformedResult {
kotlin.runCatching{ serviceX.call(input)}
.onSuccess { return functionToTranslateServiceXResponse(it)}
.onFailure{ throw functionToranslateServiceXError(it)}
}
}
functionToTranslateServiceXResponse : (OriginalResult) -> TransformedResult
functionToranslateServiceXError : (Throwable) -> RuntimeException
However Kotlin is giving me the following error - A 'return' expression required in a function with a block body ('{...}'). If you got this error after the compiler update, then it's most likely due to a fix of a bug introduced in 1.3.0 (see KT-28061 for details)
Running with Kotlin 1.4.
I am still new to Kotlin- wondering if I am doing something fundamentally wrong here. Any help would be greatly appreciated.
Update: tried the following pattern, basic test cases seem to be working fine. Would love to know what others think of the pattern
class SomeClassThatCallsServiceX {
fun remoteCall(input): TransformedResult {
return kotlin.runCatching{ serviceX.call(input)}
.mapCatching(functionToTranslateServiceXResponse)
.onFailure(functionToranslateServiceXError)
.getOrThrow()
}
}
functionToTranslateServiceXResponse : (OriginalResult) -> TransformedResult
functionToranslateServiceXError : (Throwable) -> Unit