mirror of
https://github.com/corda/corda.git
synced 2025-06-18 15:18:16 +00:00
[CORDA-1822]: Derive error code from exception signature (#3774)
This commit is contained in:
committed by
GitHub
parent
f979d9d3cf
commit
7a1b75ef35
@ -3,8 +3,6 @@ package net.corda.core.utilities
|
||||
import net.corda.core.KeepForDJVM
|
||||
import net.corda.core.internal.uncheckedCast
|
||||
import net.corda.core.serialization.CordaSerializable
|
||||
import net.corda.core.utilities.Try.Failure
|
||||
import net.corda.core.utilities.Try.Success
|
||||
|
||||
/**
|
||||
* Representation of an operation that has either succeeded with a result (represented by [Success]) or failed with an
|
||||
@ -60,6 +58,35 @@ sealed class Try<out A> {
|
||||
is Failure -> uncheckedCast(this)
|
||||
}
|
||||
|
||||
/** Applies the given action to the value if [Success], or does nothing if [Failure]. Returns `this` for chaining. */
|
||||
fun doOnSuccess(action: (A) -> Unit): Try<A> {
|
||||
when (this) {
|
||||
is Success -> action.invoke(value)
|
||||
is Failure -> {}
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
/** Applies the given action to the error if [Failure], or does nothing if [Success]. Returns `this` for chaining. */
|
||||
fun doOnFailure(action: (Throwable) -> Unit): Try<A> {
|
||||
when (this) {
|
||||
is Success -> {}
|
||||
is Failure -> action.invoke(exception)
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
/** Applies the given action to the exception if [Failure], rethrowing [Error]s. Does nothing if [Success]. Returns `this` for chaining. */
|
||||
fun doOnException(action: (Exception) -> Unit): Try<A> {
|
||||
return doOnFailure { error ->
|
||||
if (error is Exception) {
|
||||
action.invoke(error)
|
||||
} else {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@KeepForDJVM
|
||||
data class Success<out A>(val value: A) : Try<A>() {
|
||||
override val isSuccess: Boolean get() = true
|
||||
|
Reference in New Issue
Block a user