Skip to content

Commit 1fd56f2

Browse files
elizarovqwwdfsad
authored andcommitted
Properly support exception cause on JS and Native
1 parent 729dc5d commit 1fd56f2

File tree

4 files changed

+20
-77
lines changed

4 files changed

+20
-77
lines changed

kotlinx-coroutines-core/common/src/Exceptions.common.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
package kotlinx.coroutines
66

77
/**
8+
* This exception gets thrown if an exception is caught while processing [CompletionHandler] invocation for [Job].
9+
*
810
* @suppress **This an internal API and should not be used from general code.**
911
*/
1012
@InternalCoroutinesApi
11-
public expect class CompletionHandlerException(message: String, cause: Throwable) : RuntimeException
13+
public class CompletionHandlerException(message: String, cause: Throwable) : RuntimeException(message, cause)
1214

1315
public expect open class CancellationException(message: String?) : IllegalStateException
1416

15-
@Suppress("FunctionName")
17+
@Suppress("FunctionName", "NO_ACTUAL_FOR_EXPECT")
1618
public expect fun CancellationException(message: String?, cause: Throwable?) : CancellationException
1719

1820
internal expect class JobCancellationException(

kotlinx-coroutines-core/js/src/Exceptions.kt

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,18 @@
44

55
package kotlinx.coroutines
66

7-
/**
8-
* This exception gets thrown if an exception is caught while processing [CompletionHandler] invocation for [Job].
9-
*
10-
* @suppress **This an internal API and should not be used from general code.**
11-
*/
12-
@InternalCoroutinesApi
13-
public actual class CompletionHandlerException public actual constructor(
14-
message: String,
15-
public override val cause: Throwable
16-
) : RuntimeException(message.withCause(cause))
17-
187
/**
198
* Thrown by cancellable suspending functions if the [Job] of the coroutine is cancelled while it is suspending.
209
* It indicates _normal_ cancellation of a coroutine.
2110
* **It is not printed to console/log by default uncaught exception handler**.
2211
* (see [CoroutineExceptionHandler]).
2312
*/
24-
public actual open class CancellationException actual constructor(message: String?) : IllegalStateException(message)
25-
26-
/**
27-
* Creates a cancellation exception with a specified message and [cause].
28-
*/
29-
@Suppress("FunctionName")
30-
public actual fun CancellationException(message: String?, cause: Throwable?) : CancellationException =
31-
CancellationException(message.withCause(cause))
13+
public actual open class CancellationException(
14+
message: String?,
15+
cause: Throwable?
16+
) : IllegalStateException(message, cause) {
17+
actual constructor(message: String?) : this(message, null)
18+
}
3219

3320
/**
3421
* 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
3724
*/
3825
internal actual class JobCancellationException public actual constructor(
3926
message: String,
40-
public override val cause: Throwable?,
27+
cause: Throwable?,
4128
internal actual val job: Job
42-
) : CancellationException(message.withCause(cause)) {
29+
) : CancellationException(message, cause) {
4330
override fun toString(): String = "${super.toString()}; job=$job"
4431
override fun equals(other: Any?): Boolean =
4532
other === this ||
@@ -48,17 +35,6 @@ internal actual class JobCancellationException public actual constructor(
4835
(message!!.hashCode() * 31 + job.hashCode()) * 31 + (cause?.hashCode() ?: 0)
4936
}
5037

51-
@Suppress("FunctionName")
52-
internal fun IllegalStateException(message: String, cause: Throwable?) =
53-
IllegalStateException(message.withCause(cause))
54-
55-
private fun String?.withCause(cause: Throwable?) =
56-
when {
57-
cause == null -> this
58-
this == null -> "caused by $cause"
59-
else -> "$this; caused by $cause"
60-
}
61-
6238
@Suppress("NOTHING_TO_INLINE")
6339
internal actual inline fun Throwable.addSuppressedThrowable(other: Throwable) { /* empty */ }
6440

kotlinx-coroutines-core/jvm/src/Exceptions.kt

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,6 @@
66

77
package kotlinx.coroutines
88

9-
/**
10-
* This exception gets thrown if an exception is caught while processing [CompletionHandler] invocation for [Job].
11-
*
12-
* @suppress **This an internal API and should not be used from general code.**
13-
*/
14-
@InternalCoroutinesApi
15-
public actual class CompletionHandlerException actual constructor(
16-
message: String,
17-
cause: Throwable
18-
) : RuntimeException(message, cause)
19-
209
/**
2110
* Thrown by cancellable suspending functions if the [Job] of the coroutine is cancelled while it is suspending.
2211
* It indicates _normal_ cancellation of a coroutine.

kotlinx-coroutines-core/native/src/Exceptions.kt

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,18 @@
44

55
package kotlinx.coroutines
66

7-
/**
8-
* This exception gets thrown if an exception is caught while processing [CompletionHandler] invocation for [Job].
9-
*
10-
* @suppress **This an internal API and should not be used from general code.**
11-
*/
12-
@InternalCoroutinesApi
13-
public actual class CompletionHandlerException public actual constructor(
14-
message: String,
15-
public override val cause: Throwable
16-
) : RuntimeException(message.withCause(cause))
17-
187
/**
198
* Thrown by cancellable suspending functions if the [Job] of the coroutine is cancelled while it is suspending.
209
* It indicates _normal_ cancellation of a coroutine.
2110
* **It is not printed to console/log by default uncaught exception handler**.
2211
* (see [CoroutineExceptionHandler]).
2312
*/
24-
public actual open class CancellationException actual constructor(message: String?) : IllegalStateException(message)
25-
26-
/**
27-
* Creates a cancellation exception with a specified message and [cause].
28-
*/
29-
@Suppress("FunctionName")
30-
public actual fun CancellationException(message: String?, cause: Throwable?) : CancellationException =
31-
CancellationException(message.withCause(cause))
13+
public actual open class CancellationException(
14+
message: String?,
15+
cause: Throwable?
16+
) : IllegalStateException(message, cause) {
17+
actual constructor(message: String?) : this(message, null)
18+
}
3219

3320
/**
3421
* 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
3724
*/
3825
internal actual class JobCancellationException public actual constructor(
3926
message: String,
40-
public override val cause: Throwable?,
27+
cause: Throwable?,
4128
internal actual val job: Job
42-
) : CancellationException(message.withCause(cause)) {
29+
) : CancellationException(message, cause) {
4330
override fun toString(): String = "${super.toString()}; job=$job"
4431
override fun equals(other: Any?): Boolean =
4532
other === this ||
@@ -48,17 +35,6 @@ internal actual class JobCancellationException public actual constructor(
4835
(message!!.hashCode() * 31 + job.hashCode()) * 31 + (cause?.hashCode() ?: 0)
4936
}
5037

51-
@Suppress("FunctionName")
52-
internal fun IllegalStateException(message: String, cause: Throwable?) =
53-
IllegalStateException(message.withCause(cause))
54-
55-
private fun String?.withCause(cause: Throwable?) =
56-
when {
57-
cause == null -> this
58-
this == null -> "caused by $cause"
59-
else -> "$this; caused by $cause"
60-
}
61-
6238
@Suppress("NOTHING_TO_INLINE")
6339
internal actual inline fun Throwable.addSuppressedThrowable(other: Throwable) { /* empty */ }
6440

0 commit comments

Comments
 (0)