Andy McGhie
07/04/2022, 7:45 AMResult
it's often useful to map an exception to another type of exception, has there any thought been given to adding something like mapFailure
to the Result
i.e.:
inline fun <R> Result<R>.mapFailure(transform: (exception: Throwable) -> Throwable): Result<R> {
return when {
isFailure -> Result.failure(transform(exceptionOrNull()!!))
else -> this
}
}
Usage:
Result
.failure<String>(NullPointerException())
.mapFailure {
MyDomainException()
}
Mikhail
07/04/2022, 3:17 PM