Skip to content

Commit

Permalink
Property support exception cause on JS and Native
Browse files Browse the repository at this point in the history
  • Loading branch information
elizarov committed Nov 20, 2019
1 parent a1e9550 commit d2a012a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 77 deletions.
6 changes: 4 additions & 2 deletions kotlinx-coroutines-core/common/src/Exceptions.common.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
40 changes: 8 additions & 32 deletions kotlinx-coroutines-core/js/src/Exceptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 =
Expand All @@ -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 */ }

Expand Down
11 changes: 0 additions & 11 deletions kotlinx-coroutines-core/jvm/src/Exceptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
40 changes: 8 additions & 32 deletions kotlinx-coroutines-core/native/src/Exceptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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)
}
Expand Down

0 comments on commit d2a012a

Please sign in to comment.