Fix fatal error catching on init

This commit is contained in:
Him188 2020-04-08 13:58:56 +08:00
parent afb093ba44
commit da39cc41a8
2 changed files with 13 additions and 3 deletions

View File

@ -148,7 +148,12 @@ abstract class BotImpl<N : BotNetworkHandler> constructor(
}
suspend fun doInit() {
tryNTimesOrException(2) {
tryNTimesOrException(2, onRetry = {
if (!isActive) {
logger.error("Cannot init due to fatal error")
logger.error(it)
}
}) {
if (it != 0) {
logger.warning("Init failed. Retrying in 3s...")
delay(3000)
@ -156,7 +161,7 @@ abstract class BotImpl<N : BotNetworkHandler> constructor(
_network.init()
}?.let {
network.logger.error(it)
logger.error("cannot init. some features may be affected")
logger.error("Cannot init. some features may be affected")
}
}

View File

@ -16,7 +16,11 @@ internal expect fun Throwable.addSuppressedMirai(e: Throwable)
@MiraiInternalAPI
@Suppress("DuplicatedCode")
internal inline fun <R> tryNTimesOrException(repeat: Int, block: (Int) -> R): Throwable? {
internal inline fun <R> tryNTimesOrException(
repeat: Int,
onRetry: (Throwable?) -> Unit = {},
block: (Int) -> R
): Throwable? {
var lastException: Throwable? = null
repeat(repeat) {
@ -28,6 +32,7 @@ internal inline fun <R> tryNTimesOrException(repeat: Int, block: (Int) -> R): Th
lastException = e
} else lastException!!.addSuppressedMirai(e)
}
onRetry(lastException)
}
return lastException!!