mirror of
https://github.com/mamoe/mirai.git
synced 2025-03-04 15:40:13 +08:00
Unwrap NetworkException on logon failure
This commit is contained in:
parent
7739c4db80
commit
dadff42718
@ -109,8 +109,10 @@ public inline fun <R> runUnwrapCancellationException(block: () -> R): R {
|
||||
}
|
||||
}
|
||||
|
||||
public fun Throwable.unwrapCancellationException(): Throwable {
|
||||
if (this !is CancellationException) return this
|
||||
public fun Throwable.unwrapCancellationException(): Throwable = unwrap<CancellationException>()
|
||||
|
||||
public inline fun <reified E> Throwable.unwrap(): Throwable {
|
||||
if (this !is E) return this
|
||||
if (suppressedExceptions.isNotEmpty()) return this
|
||||
return this.findCause { it !is CancellationException } ?: this
|
||||
return this.findCause { it !is E } ?: this
|
||||
}
|
@ -205,6 +205,7 @@ public inline fun Throwable.findCause(maxDepth: Int = 20, filter: (Throwable) ->
|
||||
var depth = 0
|
||||
var rootCause: Throwable? = this
|
||||
while (true) {
|
||||
if (rootCause?.cause === rootCause) return rootCause
|
||||
val current = rootCause?.cause ?: return null
|
||||
if (filter(current)) return current
|
||||
rootCause = rootCause.cause
|
||||
|
@ -23,12 +23,10 @@ import net.mamoe.mirai.internal.contact.uin
|
||||
import net.mamoe.mirai.internal.network.component.ComponentStorage
|
||||
import net.mamoe.mirai.internal.network.components.SsoProcessor
|
||||
import net.mamoe.mirai.internal.network.handler.NetworkHandler
|
||||
import net.mamoe.mirai.internal.network.handler.selector.NetworkException
|
||||
import net.mamoe.mirai.internal.network.impl.netty.asCoroutineExceptionHandler
|
||||
import net.mamoe.mirai.supervisorJob
|
||||
import net.mamoe.mirai.utils.BotConfiguration
|
||||
import net.mamoe.mirai.utils.MiraiLogger
|
||||
import net.mamoe.mirai.utils.childScopeContext
|
||||
import net.mamoe.mirai.utils.info
|
||||
import net.mamoe.mirai.utils.*
|
||||
import kotlin.collections.set
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
|
||||
@ -118,10 +116,11 @@ internal abstract class AbstractBot constructor(
|
||||
try {
|
||||
network.resumeConnection()
|
||||
} catch (e: Throwable) { // failed to init
|
||||
val cause = e.unwrap<NetworkException>()
|
||||
if (!components[SsoProcessor].firstLoginSucceed) {
|
||||
this.close() // failed to do first login.
|
||||
this.close(cause) // failed to do first login.
|
||||
}
|
||||
throw e
|
||||
throw cause
|
||||
}
|
||||
logger.info { "Bot login successful." }
|
||||
}
|
||||
|
@ -12,16 +12,13 @@
|
||||
package net.mamoe.mirai.internal.network.handler
|
||||
|
||||
import io.netty.channel.Channel
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import net.mamoe.mirai.internal.network.impl.netty.AbstractNettyNHTest
|
||||
import net.mamoe.mirai.internal.network.impl.netty.TestNettyNH
|
||||
import net.mamoe.mirai.internal.test.runBlockingUnit
|
||||
import net.mamoe.mirai.utils.TestOnly
|
||||
import net.mamoe.mirai.utils.getRootCause
|
||||
import org.junit.jupiter.api.assertThrows
|
||||
import java.net.SocketAddress
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertIs
|
||||
|
||||
internal class KeepAliveNetworkHandlerSelectorRealTest : AbstractNettyNHTest() {
|
||||
|
||||
@ -42,9 +39,13 @@ internal class KeepAliveNetworkHandlerSelectorRealTest : AbstractNettyNHTest() {
|
||||
// selector should not tolerant any exception during state initialization, or in the Jobs launched in states.
|
||||
|
||||
val selector = TestSelector(3) { createHandler() }
|
||||
assertThrows<CancellationException> { selector.awaitResumeInstance() }.run {
|
||||
assertIs<MyException>(getRootCause())
|
||||
assertThrows<Throwable> { selector.awaitResumeInstance() }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `should unwrap exception`() = runBlockingUnit {
|
||||
val selector = TestSelector(3) { createHandler() }
|
||||
assertThrows<MyException> { selector.awaitResumeInstance() }
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user