Improve logging on login and init failure

This commit is contained in:
Him188 2020-12-27 18:44:57 +08:00
parent ec6e23d450
commit e38fb5c1d0

View File

@ -17,6 +17,7 @@
package net.mamoe.mirai.internal package net.mamoe.mirai.internal
import io.ktor.util.*
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.Mutex
import net.mamoe.mirai.Bot import net.mamoe.mirai.Bot
@ -208,23 +209,22 @@ internal abstract class AbstractBot<N : BotNetworkHandler> constructor(
@OptIn(ThisApiMustBeUsedInWithConnectionLockBlock::class) @OptIn(ThisApiMustBeUsedInWithConnectionLockBlock::class)
relogin(null) relogin(null)
return return
} catch (e: LoginFailedException) {
if (e.killBot) {
throw e
} else {
logger.warning { "Login failed. Retrying in 3s..." }
_network.closeAndJoin(e)
delay(3000)
continue
}
} catch (e: Exception) { } catch (e: Exception) {
network.logger.error(e) if (e is LoginFailedException) {
if (e.killBot) {
throw e
}
} else {
network.logger.error(e)
}
logger.warning { "Login failed. Retrying in 3s... (rootCause=${e.rootCause})" }
_network.closeAndJoin(e) _network.closeAndJoin(e)
delay(3000)
continue
} finally { } finally {
_isConnecting = false _isConnecting = false
} }
logger.warning { "Login failed. Retrying in 3s..." } // unreachable here
delay(3000)
} }
} }
@ -235,7 +235,7 @@ internal abstract class AbstractBot<N : BotNetworkHandler> constructor(
logger.error("Cannot init due to fatal error") logger.error("Cannot init due to fatal error")
throw lastException ?: error("<No lastException>") throw lastException ?: error("<No lastException>")
} }
logger.warning { "Init failed. Retrying in 3s..." } logger.warning { "Init failed. Retrying in 3s... (rootCause=${lastException?.rootCause})" }
delay(3000) delay(3000)
} }
@ -319,5 +319,16 @@ internal abstract class AbstractBot<N : BotNetworkHandler> constructor(
final override fun toString(): String = "Bot($id)" final override fun toString(): String = "Bot($id)"
} }
private val Throwable.rootCause: Throwable
get() {
var depth = 0
var rootCause: Throwable? = this
while (rootCause?.cause != null) {
rootCause = rootCause.cause
if (depth++ == 20) break
}
return rootCause ?: this
}
@RequiresOptIn(level = RequiresOptIn.Level.ERROR) @RequiresOptIn(level = RequiresOptIn.Level.ERROR)
internal annotation class ThisApiMustBeUsedInWithConnectionLockBlock internal annotation class ThisApiMustBeUsedInWithConnectionLockBlock