1
0
mirror of https://github.com/mamoe/mirai.git synced 2025-04-25 04:50:26 +08:00

Implement Throwable.unwrap accordingly to the target platform. Fix

This commit is contained in:
Him188 2021-07-16 15:22:31 +08:00
parent 3a9889b8c0
commit 2929db7f38
3 changed files with 24 additions and 10 deletions
mirai-core-utils/src
androidMain/kotlin
commonMain/kotlin
jvmMain/kotlin

View File

@ -21,4 +21,16 @@ public actual fun ByteArray.encodeBase64(): String {
public actual fun String.decodeBase64(): ByteArray {
return Base64.decode(this, Base64.DEFAULT)
}
}
public actual inline fun <reified E> Throwable.unwrap(): Throwable {
if (this !is E) return this
if (suppressed.isNotEmpty()) return this
val e =
Exception("unwrapped exception: $this").also { // Android JDK could not resolve circular references so we copy one.
it.stackTrace = this.stackTrace
}
return this.findCause { it !is E }
?.also { it.addSuppressed(e) }
?: this
}

View File

@ -157,13 +157,7 @@ public fun Throwable.unwrapCancellationException(): Throwable = unwrap<Cancellat
* at net.mamoe.mirai.internal.network.impl.netty.NettyNetworkHandler.close(NettyNetworkHandler.kt:404)
* ```
*/
public inline fun <reified E> 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
}
@Suppress("unused")
public expect inline fun <reified E> Throwable.unwrap(): Throwable
public val CoroutineContext.coroutineName: String get() = this[CoroutineName]?.name ?: "unnamed"

View File

@ -21,4 +21,12 @@ public actual fun ByteArray.encodeBase64(): String {
public actual fun String.decodeBase64(): ByteArray {
return Base64.getDecoder().decode(this)
}
}
public actual inline fun <reified E> 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
}