Return a new failure from Result.onFailure

Hi,

I’m doing something like this:

kotlin.runCatching {
    ...
    if(something())
        throw NotAuthorizedException("Bad user.")
    ...
}.onFailure {
    logger.error("User was bad.", it)

    if (it is NotAuthorizedException)
        return Result.failure(DepositAccountException.ClientErrorException("User was evil."))
}

Is this OK to do?

The definition of the Result.onFailure says it returns the original failure, but here I am overwriting that.
It seems to work fine, but I wanted to know if this was bad practice.

This code doesn’t do what you probably think it does. return does not somehow replace the result of onFailure(). It returns from the nearest enclosing function.

You can use fold() or create an extension, something like mapFailure().