diff --git a/mirai-core-utils/src/androidMain/kotlin/Actuals.kt b/mirai-core-utils/src/androidMain/kotlin/Actuals.kt index 0d090bd76..9820884c4 100644 --- a/mirai-core-utils/src/androidMain/kotlin/Actuals.kt +++ b/mirai-core-utils/src/androidMain/kotlin/Actuals.kt @@ -32,8 +32,10 @@ internal class StacktraceException(override val message: String?, private val st public actual inline fun Throwable.unwrap(): Throwable { if (this !is E) return this - if (suppressed.isNotEmpty()) return this val e = StacktraceException("Unwrapped exception: $this", this.stackTrace) + for (throwable in this.suppressed) { + e.addSuppressed(throwable) + } return this.findCause { it !is E } ?.also { it.addSuppressed(e) } ?: this diff --git a/mirai-core-utils/src/androidTest/kotlin/AndroidUnwrapTest.kt b/mirai-core-utils/src/androidTest/kotlin/AndroidUnwrapTest.kt new file mode 100644 index 000000000..e26a90747 --- /dev/null +++ b/mirai-core-utils/src/androidTest/kotlin/AndroidUnwrapTest.kt @@ -0,0 +1,32 @@ +/* + * Copyright 2019-2021 Mamoe Technologies and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. + * + * https://github.com/mamoe/mirai/blob/dev/LICENSE + */ + +package net.mamoe.mirai.utils + +import kotlin.test.Test +import kotlin.test.assertIs + +class AndroidUnwrapTest { + @Test + fun test() { + val e = + IllegalStateException( + OutOfMemoryError().initCause(VerifyError()).apply { addSuppressed(UnsatisfiedLinkError()) } + ) + + val unwrapped = e.unwrap() + + unwrapped.printStackTrace() + + assertIs(unwrapped) + assertIs(unwrapped.cause) + assertIs(unwrapped.suppressed.first()) + assertIs(unwrapped.suppressed[1]) + } +} \ No newline at end of file diff --git a/mirai-core-utils/src/jvmMain/kotlin/Actuals.kt b/mirai-core-utils/src/jvmMain/kotlin/Actuals.kt index 5a04576ac..c2e695703 100644 --- a/mirai-core-utils/src/jvmMain/kotlin/Actuals.kt +++ b/mirai-core-utils/src/jvmMain/kotlin/Actuals.kt @@ -25,7 +25,6 @@ public actual fun String.decodeBase64(): ByteArray { public actual inline fun Throwable.unwrap(): Throwable { if (this !is E) return this - if (suppressed.isNotEmpty()) return this return this.findCause { it !is E } ?.also { it.addSuppressed(this) } ?: this diff --git a/mirai-core-utils/src/jvmTest/kotlin/AndroidUnwrapTest.kt b/mirai-core-utils/src/jvmTest/kotlin/AndroidUnwrapTest.kt new file mode 100644 index 000000000..4e70703e4 --- /dev/null +++ b/mirai-core-utils/src/jvmTest/kotlin/AndroidUnwrapTest.kt @@ -0,0 +1,32 @@ +/* + * Copyright 2019-2021 Mamoe Technologies and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. + * + * https://github.com/mamoe/mirai/blob/dev/LICENSE + */ + +package net.mamoe.mirai.utils + +import kotlin.test.Test +import kotlin.test.assertIs + +class JvmUnwrapTest { + @Test + fun test() { + val e = + IllegalStateException( + OutOfMemoryError().initCause(VerifyError()).apply { addSuppressed(UnsatisfiedLinkError()) } + ) + + val unwrapped = e.unwrap() + + unwrapped.printStackTrace() + + assertIs(unwrapped) + assertIs(unwrapped.cause) + assertIs(unwrapped.suppressed.first()) + assertIs(unwrapped.suppressed[1]) + } +} \ No newline at end of file