[history] Added functions that passes thrown exception to 'onError' block

This commit is contained in:
Andrew Golovashevich 2025-03-19 00:37:46 +03:00
parent 6f9d3bdd32
commit b2a5ac9292

View File

@ -29,6 +29,21 @@ inline fun <R> safeAutoClose2(
return safeAutoClose3(onError = onError, onSuccess = onSuccess, onCrossReturn = onSuccess, action = action)
}
inline fun <R> safeAutoClose2e(
onError: (Throwable) -> Unit = {},
onSuccess: () -> Unit = {},
action: () -> R
): R {
@Suppress("WRONG_INVOCATION_KIND")
contract {
callsInPlace(action, InvocationKind.EXACTLY_ONCE)
callsInPlace(onError, InvocationKind.AT_MOST_ONCE)
callsInPlace(onSuccess, InvocationKind.AT_MOST_ONCE)
}
return safeAutoClose3e(onError = onError, onSuccess = onSuccess, onCrossReturn = onSuccess, action = action)
}
inline fun <R> safeAutoClose3(
onError: () -> Unit = {},
onSuccess: () -> Unit = {},
@ -41,6 +56,21 @@ inline fun <R> safeAutoClose3(
callsInPlace(onSuccess, InvocationKind.AT_MOST_ONCE)
callsInPlace(onCrossReturn, InvocationKind.AT_MOST_ONCE)
}
return safeAutoClose3e(onError = { t -> onError() }, onSuccess = onSuccess, onCrossReturn = onCrossReturn, action = action)
}
inline fun <R> safeAutoClose3e(
onError: (Throwable) -> Unit = {},
onSuccess: () -> Unit = {},
onCrossReturn: () -> Unit = {},
action: () -> R
): R {
contract {
callsInPlace(action, InvocationKind.EXACTLY_ONCE)
callsInPlace(onError, InvocationKind.AT_MOST_ONCE)
callsInPlace(onSuccess, InvocationKind.AT_MOST_ONCE)
callsInPlace(onCrossReturn, InvocationKind.AT_MOST_ONCE)
}
val ret: R
var wasError = false
@ -51,7 +81,7 @@ inline fun <R> safeAutoClose3(
} catch (e1: Throwable) {
wasError = true
try {
onError()
onError(e1)
} catch (e2: Throwable) {
e1.addSuppressed(e2)
}