CatchingResult

Result 的包装,泛型 T 在类级别,提供成员函数来弥补 Result 错误处理方面的缺陷,并转发 Result 的标准 API。 为了避免 <E, T> 导致编译器无法推断泛型类型,包装 Result,成员函数就不需要 T 了。

Properties

Link copied to clipboard
Link copied to clipboard

Functions

Link copied to clipboard
Link copied to clipboard
inline fun <R> fold(onSuccess: (T) -> R, onFailure: (Throwable) -> R): R
Link copied to clipboard
fun <R, T : R> CatchingResult<T>.getOrDefault(defaultValue: R): R
Link copied to clipboard
inline fun <R, T : R> CatchingResult<T>.getOrElse(onFailure: (Throwable) -> R): R
Link copied to clipboard
fun getOrNull(): T?
Link copied to clipboard
fun getOrThrow(): T
Link copied to clipboard
inline fun <R> map(transform: (T) -> R): CatchingResult<R>
Link copied to clipboard
inline fun <E : Throwable> onException(action: (E) -> Unit): CatchingResult<T>

如果异常为 E,执行 action

Link copied to clipboard
inline fun <E : Throwable> onExceptionExcept(action: (Throwable) -> Unit): CatchingResult<T>

如果异常不是 E,执行 action

Link copied to clipboard
inline fun onFailure(action: (Throwable) -> Unit): CatchingResult<T>
Link copied to clipboard
inline fun onFailureIf(test: (Throwable) -> Boolean, action: (Throwable) -> Unit): CatchingResult<T>

如果 test,执行 action

Link copied to clipboard
inline fun onSuccess(action: (T) -> Unit): CatchingResult<T>
Link copied to clipboard
inline fun <R, T : R> CatchingResult<T>.recover(transform: (Throwable) -> R): CatchingResult<R>
Link copied to clipboard
inline fun <E : Throwable, R, T : R> CatchingResult<T>.recoverException(transform: (E) -> R): CatchingResult<Any?>

如果异常为 E,对 T 进行 transform,返回新的 CatchingResult

Link copied to clipboard
inline fun <R, T : R> CatchingResult<T>.recoverIf(test: (Throwable) -> Boolean, transform: (Throwable) -> R): CatchingResult<Any?>

如果 test,对 T 进行 transform,返回新的 CatchingResult

Link copied to clipboard
inline fun <E : Throwable> rethrow(): CatchingResult<T>

重新抛出类型为 E 的异常,请使用 rethrowCancellation 替代 rethrow<CancellationException>()

inline fun <E : Throwable> rethrow(on: (E) -> Unit): CatchingResult<T>

如果异常为 E,执行 on 并抛出异常。请使用 rethrowCancellation 替代 rethrow<CancellationException>()

Link copied to clipboard

重新抛出类型为 CancellationException 的异常

fun rethrowCancellation(on: (CancellationException) -> Unit): CatchingResult<T>

如果异常为 CancellationException,执行 on 并抛出 CancellationException

Link copied to clipboard
inline fun rethrowIf(test: (Throwable) -> Boolean): CatchingResult<T>

如果 test,重新抛出异常

inline fun rethrowIf(test: (Throwable) -> Boolean, on: (Throwable) -> Unit): CatchingResult<T>

如果 test,执行 on 并重新抛出异常

Link copied to clipboard

如果异常不是 E,重新抛出