|
| 1 | +/* |
| 2 | + * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
| 3 | + */ |
| 4 | + |
| 5 | +package kotlinx.coroutines.internal |
| 6 | + |
| 7 | +import kotlinx.coroutines.* |
| 8 | +import java.util.* |
| 9 | +import kotlin.coroutines.* |
| 10 | +import kotlin.coroutines.intrinsics.* |
| 11 | + |
| 12 | +internal actual fun <E : Throwable> recoverStackTrace(exception: E): E { |
| 13 | + if (recoveryDisabled(exception)) { |
| 14 | + return exception |
| 15 | + } |
| 16 | + |
| 17 | + val copy = tryCopyException(exception) ?: return exception |
| 18 | + return copy.sanitizeStackTrace() |
| 19 | +} |
| 20 | + |
| 21 | +private fun <E : Throwable> E.sanitizeStackTrace(): E { |
| 22 | + val size = stackTrace.size |
| 23 | + |
| 24 | + var lastIntrinsic = -1 |
| 25 | + for (i in 0 until size) { |
| 26 | + val name = stackTrace[i].className |
| 27 | + if ("kotlinx.coroutines.internal.ExceptionsKt" == name) { |
| 28 | + lastIntrinsic = i |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + val startIndex = lastIntrinsic + 1 |
| 33 | + val trace = Array(size - lastIntrinsic) { |
| 34 | + if (it == 0) { |
| 35 | + artificialFrame("Current coroutine stacktrace") |
| 36 | + } else { |
| 37 | + stackTrace[startIndex + it - 1] |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + stackTrace = trace |
| 42 | + return this |
| 43 | +} |
| 44 | + |
| 45 | +internal actual fun <E : Throwable> recoverStackTrace(exception: E, continuation: Continuation<*>): E { |
| 46 | + if (recoveryDisabled(exception) || continuation !is CoroutineStackFrame) { |
| 47 | + return exception |
| 48 | + } |
| 49 | + |
| 50 | + return recoverFromStackFrame(exception, continuation) |
| 51 | +} |
| 52 | + |
| 53 | +private fun <E : Throwable> recoverFromStackFrame(exception: E, continuation: CoroutineStackFrame): E { |
| 54 | + val newException = tryCopyException(exception) ?: return exception |
| 55 | + val stacktrace = createStackTrace(continuation) |
| 56 | + if (stacktrace.isEmpty()) return exception |
| 57 | + stacktrace.add(0, artificialFrame("Current coroutine stacktrace")) |
| 58 | + newException.stackTrace = stacktrace.toTypedArray() |
| 59 | + return newException |
| 60 | +} |
| 61 | + |
| 62 | + |
| 63 | +@Suppress("NOTHING_TO_INLINE") |
| 64 | +internal actual suspend inline fun recoverAndThrow(exception: Throwable): Nothing { |
| 65 | + if (recoveryDisabled(exception)) throw exception |
| 66 | + suspendCoroutineUninterceptedOrReturn<Nothing> { |
| 67 | + if (it !is CoroutineStackFrame) throw exception |
| 68 | + throw recoverFromStackFrame(exception, it) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +internal actual fun <E : Throwable> unwrap(exception: E): E { |
| 73 | + if (recoveryDisabled(exception)) { |
| 74 | + return exception |
| 75 | + } |
| 76 | + |
| 77 | + val element = exception.stackTrace.firstOrNull() ?: return exception |
| 78 | + if (element.isArtificial()) { |
| 79 | + @Suppress("UNCHECKED_CAST") |
| 80 | + return exception.cause as? E ?: exception |
| 81 | + } else { |
| 82 | + return exception |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +private fun <E : Throwable> recoveryDisabled(exception: E) = |
| 87 | + !RECOVER_STACKTRACE || !DEBUG || exception is CancellationException || exception is NonRecoverableThrowable |
| 88 | + |
| 89 | +@Suppress("UNCHECKED_CAST") |
| 90 | +private fun <E : Throwable> tryCopyException(exception: E): E? { |
| 91 | + /* |
| 92 | + * Try to reflectively find constructor(), constructor(message, cause) or constructor(cause). |
| 93 | + * Exceptions are shared among coroutines, so we should copy exception before recovering current stacktrace. |
| 94 | + */ |
| 95 | + var newException: E? = null |
| 96 | + try { |
| 97 | + val constructors = exception.javaClass.constructors.sortedByDescending { it.parameterTypes.size } |
| 98 | + for (constructor in constructors) { |
| 99 | + val parameters = constructor.parameterTypes |
| 100 | + if (parameters.size == 2 && parameters[0] == String::class.java && parameters[1] == Throwable::class.java) { |
| 101 | + newException = constructor.newInstance(exception.message, exception) as E |
| 102 | + } else if (parameters.size == 1 && parameters[0] == Throwable::class.java) { |
| 103 | + newException = constructor.newInstance(exception) as E |
| 104 | + } else if (parameters.isEmpty()) { |
| 105 | + newException = (constructor.newInstance() as E).also { it.initCause(exception) } |
| 106 | + } |
| 107 | + |
| 108 | + if (newException != null) { |
| 109 | + break |
| 110 | + } |
| 111 | + } |
| 112 | + } catch (e: Exception) { |
| 113 | + // Do nothing |
| 114 | + } |
| 115 | + return newException |
| 116 | +} |
| 117 | + |
| 118 | +private fun createStackTrace(continuation: CoroutineStackFrame): ArrayList<StackTraceElement> { |
| 119 | + val stack = ArrayList<StackTraceElement>() |
| 120 | + continuation.getStackTraceElement()?.let { stack.add(it) } |
| 121 | + |
| 122 | + var last = continuation |
| 123 | + while (true) { |
| 124 | + last = (last as? CoroutineStackFrame)?.callerFrame ?: break |
| 125 | + last.getStackTraceElement()?.let { stack.add(it) } |
| 126 | + } |
| 127 | + return stack |
| 128 | +} |
| 129 | + |
| 130 | + |
| 131 | +internal fun artificialFrame(message: String) = java.lang.StackTraceElement("\b\b\b($message", "\b", "\b", -1) |
| 132 | +internal fun StackTraceElement.isArtificial() = className.startsWith("\b\b\b") |
| 133 | + |
| 134 | +@Suppress("ACTUAL_WITHOUT_EXPECT") |
| 135 | +actual typealias CoroutineStackFrame = kotlin.coroutines.jvm.internal.CoroutineStackFrame |
| 136 | + |
| 137 | +actual typealias StackTraceElement = java.lang.StackTraceElement |
0 commit comments