mirror of
https://github.com/mamoe/mirai.git
synced 2025-03-14 07:10:09 +08:00
Fix fatal error catching on init
This commit is contained in:
parent
afb093ba44
commit
da39cc41a8
@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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!!
|
||||
|
Loading…
Reference in New Issue
Block a user