From d2a012a3bdab40b2bb1bc97c807070c937b49139 Mon Sep 17 00:00:00 2001 From: Roman Elizarov Date: Wed, 20 Nov 2019 17:52:23 +0300 Subject: [PATCH] Property support exception cause on JS and Native --- .../common/src/Exceptions.common.kt | 6 ++- kotlinx-coroutines-core/js/src/Exceptions.kt | 40 ++++--------------- kotlinx-coroutines-core/jvm/src/Exceptions.kt | 11 ----- .../native/src/Exceptions.kt | 40 ++++--------------- 4 files changed, 20 insertions(+), 77 deletions(-) diff --git a/kotlinx-coroutines-core/common/src/Exceptions.common.kt b/kotlinx-coroutines-core/common/src/Exceptions.common.kt index 4d41e533eb..9b389502f6 100644 --- a/kotlinx-coroutines-core/common/src/Exceptions.common.kt +++ b/kotlinx-coroutines-core/common/src/Exceptions.common.kt @@ -5,14 +5,16 @@ package kotlinx.coroutines /** + * This exception gets thrown if an exception is caught while processing [CompletionHandler] invocation for [Job]. + * * @suppress **This an internal API and should not be used from general code.** */ @InternalCoroutinesApi -public expect class CompletionHandlerException(message: String, cause: Throwable) : RuntimeException +public class CompletionHandlerException(message: String, cause: Throwable) : RuntimeException(message, cause) public expect open class CancellationException(message: String?) : IllegalStateException -@Suppress("FunctionName") +@Suppress("FunctionName", "NO_ACTUAL_FOR_EXPECT") public expect fun CancellationException(message: String?, cause: Throwable?) : CancellationException internal expect class JobCancellationException( diff --git a/kotlinx-coroutines-core/js/src/Exceptions.kt b/kotlinx-coroutines-core/js/src/Exceptions.kt index d5c337ebb9..9d2e212499 100644 --- a/kotlinx-coroutines-core/js/src/Exceptions.kt +++ b/kotlinx-coroutines-core/js/src/Exceptions.kt @@ -4,31 +4,18 @@ package kotlinx.coroutines -/** - * This exception gets thrown if an exception is caught while processing [CompletionHandler] invocation for [Job]. - * - * @suppress **This an internal API and should not be used from general code.** - */ -@InternalCoroutinesApi -public actual class CompletionHandlerException public actual constructor( - message: String, - public override val cause: Throwable -) : RuntimeException(message.withCause(cause)) - /** * Thrown by cancellable suspending functions if the [Job] of the coroutine is cancelled while it is suspending. * It indicates _normal_ cancellation of a coroutine. * **It is not printed to console/log by default uncaught exception handler**. * (see [CoroutineExceptionHandler]). */ -public actual open class CancellationException actual constructor(message: String?) : IllegalStateException(message) - -/** - * Creates a cancellation exception with a specified message and [cause]. - */ -@Suppress("FunctionName") -public actual fun CancellationException(message: String?, cause: Throwable?) : CancellationException = - CancellationException(message.withCause(cause)) +public actual open class CancellationException( + message: String?, + cause: Throwable? +) : IllegalStateException(message, cause) { + actual constructor(message: String?) : this(message, null) +} /** * Thrown by cancellable suspending functions if the [Job] of the coroutine is cancelled or completed @@ -37,9 +24,9 @@ public actual fun CancellationException(message: String?, cause: Throwable?) : C */ internal actual class JobCancellationException public actual constructor( message: String, - public override val cause: Throwable?, + cause: Throwable?, job: Job -) : CancellationException(message.withCause(cause)) { +) : CancellationException(message, cause) { internal actual val job: Job? = job override fun toString(): String = "${super.toString()}; job=$job" override fun equals(other: Any?): Boolean = @@ -49,17 +36,6 @@ internal actual class JobCancellationException public actual constructor( (message!!.hashCode() * 31 + job.hashCode()) * 31 + (cause?.hashCode() ?: 0) } -@Suppress("FunctionName") -internal fun IllegalStateException(message: String, cause: Throwable?) = - IllegalStateException(message.withCause(cause)) - -private fun String?.withCause(cause: Throwable?) = - when { - cause == null -> this - this == null -> "caused by $cause" - else -> "$this; caused by $cause" - } - @Suppress("NOTHING_TO_INLINE") internal actual inline fun Throwable.addSuppressedThrowable(other: Throwable) { /* empty */ } diff --git a/kotlinx-coroutines-core/jvm/src/Exceptions.kt b/kotlinx-coroutines-core/jvm/src/Exceptions.kt index dc726a7cb6..737e432620 100644 --- a/kotlinx-coroutines-core/jvm/src/Exceptions.kt +++ b/kotlinx-coroutines-core/jvm/src/Exceptions.kt @@ -6,17 +6,6 @@ package kotlinx.coroutines -/** - * This exception gets thrown if an exception is caught while processing [CompletionHandler] invocation for [Job]. - * - * @suppress **This an internal API and should not be used from general code.** - */ -@InternalCoroutinesApi -public actual class CompletionHandlerException actual constructor( - message: String, - cause: Throwable -) : RuntimeException(message, cause) - /** * Thrown by cancellable suspending functions if the [Job] of the coroutine is cancelled while it is suspending. * It indicates _normal_ cancellation of a coroutine. diff --git a/kotlinx-coroutines-core/native/src/Exceptions.kt b/kotlinx-coroutines-core/native/src/Exceptions.kt index 7660a41d20..0fc4cb7250 100644 --- a/kotlinx-coroutines-core/native/src/Exceptions.kt +++ b/kotlinx-coroutines-core/native/src/Exceptions.kt @@ -8,31 +8,18 @@ import kotlinx.atomicfu.* import kotlinx.coroutines.internal.* import kotlin.native.ref.* -/** - * This exception gets thrown if an exception is caught while processing [CompletionHandler] invocation for [Job]. - * - * @suppress **This an internal API and should not be used from general code.** - */ -@InternalCoroutinesApi -public actual class CompletionHandlerException public actual constructor( - message: String, - public override val cause: Throwable -) : RuntimeException(message.withCause(cause)) - /** * Thrown by cancellable suspending functions if the [Job] of the coroutine is cancelled while it is suspending. * It indicates _normal_ cancellation of a coroutine. * **It is not printed to console/log by default uncaught exception handler**. * (see [CoroutineExceptionHandler]). */ -public actual open class CancellationException actual constructor(message: String?) : IllegalStateException(message) - -/** - * Creates a cancellation exception with a specified message and [cause]. - */ -@Suppress("FunctionName") -public actual fun CancellationException(message: String?, cause: Throwable?) : CancellationException = - CancellationException(message.withCause(cause)) +public actual open class CancellationException( + message: String?, + cause: Throwable? +) : IllegalStateException(message, cause) { + actual constructor(message: String?) : this(message, null) +} /** * Thrown by cancellable suspending functions if the [Job] of the coroutine is cancelled or completed @@ -41,9 +28,9 @@ public actual fun CancellationException(message: String?, cause: Throwable?) : C */ internal actual class JobCancellationException public actual constructor( message: String, - public override val cause: Throwable?, + cause: Throwable?, job: Job -) : CancellationException(message.withCause(cause)) { +) : CancellationException(message, cause) { private val ref = WeakReference(job) internal actual val job: Job? get() = ref.get() @@ -56,17 +43,6 @@ internal actual class JobCancellationException public actual constructor( (message!!.hashCode() * 31 + job.hashCode()) * 31 + (cause?.hashCode() ?: 0) } -@Suppress("FunctionName") -internal fun IllegalStateException(message: String, cause: Throwable?) = - IllegalStateException(message.withCause(cause)) - -private fun String?.withCause(cause: Throwable?) = - when { - cause == null -> this - this == null -> "caused by $cause" - else -> "$this; caused by $cause" - } - internal actual fun Throwable.addSuppressedThrowable(other: Throwable) { if (this is SuppressSupportingThrowableImpl) addSuppressed(other) }