Skip to content

Commit

Permalink
Properly support exception cause on JS and Native
Browse files Browse the repository at this point in the history
  • Loading branch information
elizarov authored and qwwdfsad committed Nov 21, 2019
1 parent 729dc5d commit 1fd56f2
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?,
internal actual val job: Job
) : CancellationException(message.withCause(cause)) {
) : CancellationException(message, cause) {
override fun toString(): String = "${super.toString()}; job=$job"
override fun equals(other: Any?): Boolean =
other === this ||
Expand All @@ -48,17 +35,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 @@ -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?,
internal actual val job: Job
) : CancellationException(message.withCause(cause)) {
) : CancellationException(message, cause) {
override fun toString(): String = "${super.toString()}; job=$job"
override fun equals(other: Any?): Boolean =
other === this ||
Expand All @@ -48,17 +35,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

0 comments on commit 1fd56f2

Please sign in to comment.