Skip to content

Commit 00b2ff4

Browse files
committed
Removed redundant ManualCancellationException
1 parent e18b96b commit 00b2ff4

File tree

8 files changed

+28
-30
lines changed

8 files changed

+28
-30
lines changed

krpc/krpc-client/src/commonMain/kotlin/kotlinx/rpc/krpc/client/KrpcClient.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ public abstract class KrpcClient : RpcClient, KrpcEndpoint {
484484
} catch (e: CancellationException) {
485485
internalScope.ensureActive()
486486

487-
failure = ManualCancellationException(e)
487+
failure = e
488488

489489
// stop the flow and its coroutine, other flows are not affected
490490
throw e

krpc/krpc-core/src/commonMain/kotlin/kotlinx/rpc/krpc/internal/ExceptionUtils.kt

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ public fun serializeException(cause: Throwable): SerializedException {
1313
val message = cause.message ?: "Unknown exception"
1414
val stacktrace = cause.stackElements()
1515
val serializedCause = cause.cause?.let { serializeException(it) }
16-
val className = cause::class.rpcInternalTypeName ?: ""
16+
val className = if (cause is CancellationException || cause is kotlin.coroutines.cancellation.CancellationException) {
17+
CancellationException::class.rpcInternalTypeName ?: "kotlinx.coroutines.CancellationException"
18+
} else {
19+
cause::class.rpcInternalTypeName ?: ""
20+
}
1721

1822
return SerializedException(cause.toString(), message, stacktrace, serializedCause, className)
1923
}
@@ -22,16 +26,15 @@ internal expect fun Throwable.stackElements(): List<StackElement>
2226

2327
internal expect fun SerializedException.deserializeUnsafe(): Throwable
2428

25-
internal fun SerializedException.nonJvmManualCancellationExceptionDeserialize(): ManualCancellationException? {
26-
if (className == ManualCancellationException::class.rpcInternalTypeName) {
27-
val cancellation = cause?.deserializeUnsafe()
28-
?: error("ManualCancellationException must have a cause")
29+
internal fun SerializedException.cancellationExceptionDeserialize(): CancellationException? {
30+
if (className == CancellationException::class.rpcInternalTypeName
31+
|| className == kotlin.coroutines.cancellation.CancellationException::class.rpcInternalTypeName
32+
) {
33+
val cause = this@cancellationExceptionDeserialize.cause?.deserializeUnsafe()
2934

30-
return ManualCancellationException(
31-
CancellationException(
32-
message = cancellation.message,
33-
cause = cancellation.cause,
34-
)
35+
return CancellationException(
36+
message = message,
37+
cause = cause,
3538
)
3639
}
3740

@@ -44,29 +47,19 @@ public fun SerializedException.deserialize(): Throwable {
4447
deserializeUnsafe()
4548
}
4649

47-
val result = if (cause.isFailure) {
50+
return if (cause.isFailure) {
4851
cause.exceptionOrNull()!!
4952
} else {
50-
val ex = cause.getOrNull()!!
51-
if (ex is ManualCancellationException) {
52-
ex.cause
53-
} else {
54-
ex
55-
}
53+
cause.getOrNull()!!
5654
}
57-
58-
return result
5955
}
6056

61-
@InternalRpcApi
62-
public class ManualCancellationException(override val cause: CancellationException): RuntimeException()
63-
6457
internal expect class DeserializedException(
6558
toStringMessage: String,
6659
message: String,
6760
stacktrace: List<StackElement>,
6861
cause: SerializedException?,
69-
className: String
62+
className: String,
7063
) : Throwable {
7164
override val message: String
7265
}

krpc/krpc-core/src/jsMain/kotlin/kotlinx/rpc/krpc/internal/ExceptionUtils.js.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ internal actual class DeserializedException actual constructor(
2323
internal actual fun Throwable.stackElements(): List<StackElement> = emptyList()
2424

2525
internal actual fun SerializedException.deserializeUnsafe(): Throwable {
26-
return nonJvmManualCancellationExceptionDeserialize()
26+
return cancellationExceptionDeserialize()
2727
?: DeserializedException(toStringMessage, message, stacktrace, cause, className)
2828
}

krpc/krpc-core/src/jvmMain/kotlin/kotlinx/rpc/krpc/internal/ExceptionUtils.jvm.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ internal actual fun Throwable.stackElements(): List<StackElement> = stackTrace.m
3737
}
3838

3939
internal actual fun SerializedException.deserializeUnsafe(): Throwable {
40+
val cancellationException = cancellationExceptionDeserialize()
41+
if (cancellationException != null) {
42+
return cancellationException
43+
}
44+
4045
try {
4146
val clazz = Class.forName(className)
4247
val fieldsCount = clazz.fieldsCountOrDefault(throwableFields)

krpc/krpc-core/src/nativeMain/kotlin/kotlinx/rpc/krpc/internal/ExceptionUtils.native.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ internal actual class DeserializedException actual constructor(
2222
internal actual fun Throwable.stackElements(): List<StackElement> = emptyList()
2323

2424
internal actual fun SerializedException.deserializeUnsafe(): Throwable {
25-
return nonJvmManualCancellationExceptionDeserialize()
25+
return cancellationExceptionDeserialize()
2626
?: DeserializedException(toStringMessage, message, stacktrace, cause, className)
2727
}

krpc/krpc-core/src/wasmJsMain/kotlin/kotlinx/rpc/krpc/internal/ExceptionUtils.wasm.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ internal actual class DeserializedException actual constructor(
2323
internal actual fun Throwable.stackElements(): List<StackElement> = emptyList()
2424

2525
internal actual fun SerializedException.deserializeUnsafe(): Throwable {
26-
return nonJvmManualCancellationExceptionDeserialize()
26+
return cancellationExceptionDeserialize()
2727
?: DeserializedException(toStringMessage, message, stacktrace, cause, className)
2828
}

krpc/krpc-core/src/wasmWasiMain/kotlin/kotlinx/rpc/krpc/internal/ExceptionUtils.wasmWasi.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ internal actual class DeserializedException actual constructor(
2020
internal actual fun Throwable.stackElements(): List<StackElement> = emptyList()
2121

2222
internal actual fun SerializedException.deserializeUnsafe(): Throwable {
23-
return nonJvmManualCancellationExceptionDeserialize()
23+
return cancellationExceptionDeserialize()
2424
?: DeserializedException(toStringMessage, message, stacktrace, cause, className)
2525
}

krpc/krpc-server/src/commonMain/kotlin/kotlinx/rpc/krpc/server/internal/KrpcServerService.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ internal class KrpcServerService<@Rpc T : Any>(
193193
throw cause
194194
}
195195

196-
failure = ManualCancellationException(cause)
196+
failure = cause
197197

198198
throw cause
199199
} catch (cause: Throwable) {
@@ -332,7 +332,7 @@ internal class KrpcServerService<@Rpc T : Any>(
332332
throw cause
333333
}
334334

335-
failure = ManualCancellationException(cause)
335+
failure = cause
336336

337337
throw cause
338338
} catch (cause: Throwable) {

0 commit comments

Comments
 (0)