Commit da39cc41 authored by Him188's avatar Him188

Fix fatal error catching on init

parent afb093ba
...@@ -148,7 +148,12 @@ abstract class BotImpl<N : BotNetworkHandler> constructor( ...@@ -148,7 +148,12 @@ abstract class BotImpl<N : BotNetworkHandler> constructor(
} }
suspend fun doInit() { suspend fun doInit() {
tryNTimesOrException(2) { tryNTimesOrException(2, onRetry = {
if (!isActive) {
logger.error("Cannot init due to fatal error")
logger.error(it)
}
}) {
if (it != 0) { if (it != 0) {
logger.warning("Init failed. Retrying in 3s...") logger.warning("Init failed. Retrying in 3s...")
delay(3000) delay(3000)
...@@ -156,7 +161,7 @@ abstract class BotImpl<N : BotNetworkHandler> constructor( ...@@ -156,7 +161,7 @@ abstract class BotImpl<N : BotNetworkHandler> constructor(
_network.init() _network.init()
}?.let { }?.let {
network.logger.error(it) network.logger.error(it)
logger.error("cannot init. some features may be affected") logger.error("Cannot init. some features may be affected")
} }
} }
......
...@@ -16,7 +16,11 @@ internal expect fun Throwable.addSuppressedMirai(e: Throwable) ...@@ -16,7 +16,11 @@ internal expect fun Throwable.addSuppressedMirai(e: Throwable)
@MiraiInternalAPI @MiraiInternalAPI
@Suppress("DuplicatedCode") @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 var lastException: Throwable? = null
repeat(repeat) { repeat(repeat) {
...@@ -28,6 +32,7 @@ internal inline fun <R> tryNTimesOrException(repeat: Int, block: (Int) -> R): Th ...@@ -28,6 +32,7 @@ internal inline fun <R> tryNTimesOrException(repeat: Int, block: (Int) -> R): Th
lastException = e lastException = e
} else lastException!!.addSuppressedMirai(e) } else lastException!!.addSuppressedMirai(e)
} }
onRetry(lastException)
} }
return lastException!! return lastException!!
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment