Skip to content
This repository was archived by the owner on Jan 28, 2025. It is now read-only.

Commit dfeb43f

Browse files
author
Gregory Lureau
committed
v0.4.1 Expose CancellationException.
1 parent a08b944 commit dfeb43f

File tree

6 files changed

+61
-1
lines changed

6 files changed

+61
-1
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ plugins {
2424

2525
allprojects {
2626
group = "deezer.kustomexport"
27-
version = "0.4.0"
27+
version = "0.4.1"
2828

2929
repositories {
3030
mavenLocal()

compiler/src/main/kotlin/deezer/kustomexport/compiler/js/Exceptions.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ val CLASS_CAST_EXCEPTION = ClassName("kotlin", "ClassCastException")
3737
val ASSERTION_ERROR = ClassName("kotlin", "AssertionError")
3838
val NO_SUCH_ELEMENT_EXCEPTION = ClassName("kotlin", "NoSuchElementException")
3939
val ARITHMETIC_EXCEPTION = ClassName("kotlin", "ArithmeticException")
40+
// Coroutines
41+
val CANCELLATION_EXCEPTION = ClassName("kotlinx.coroutines", "CancellationException")
42+
val TIMEOUT_CANCELLATION_EXCEPTION = ClassName("kotlinx.coroutines", "TimeoutCancellationException")
4043

4144
const val EXCEPTION_JS_PACKAGE = "deezer.kustomexport"
4245
fun TypeName.toJsException(): ClassName = ClassName(EXCEPTION_JS_PACKAGE, (this as ClassName).simpleName)
@@ -58,4 +61,6 @@ val ALL_KOTLIN_EXCEPTIONS = listOf(
5861
ASSERTION_ERROR,
5962
NO_SUCH_ELEMENT_EXCEPTION,
6063
ARITHMETIC_EXCEPTION,
64+
CANCELLATION_EXCEPTION,
65+
TIMEOUT_CANCELLATION_EXCEPTION
6166
)

lib-coroutines/build.gradle.kts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ kotlin {
1717
languageSettings.optIn("kotlin.RequiresOptIn")
1818
languageSettings.optIn("kotlin.js.ExperimentalJsExport")
1919
}
20+
val commonMain by getting {
21+
dependencies {
22+
implementation(project(":lib"))
23+
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0")
24+
}
25+
}
2026
}
2127

2228
targets.all {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2022 Deezer.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing,
11+
* software distributed under the License is distributed on an
12+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13+
* KIND, either express or implied. See the License for the
14+
* specific language governing permissions and limitations
15+
* under the License.
16+
*/
17+
18+
@file:Suppress("NON_EXPORTABLE_TYPE")
19+
@file:OptIn(ExperimentalJsExport::class)
20+
21+
package deezer.kustomexport
22+
23+
import kotlin.js.ExperimentalJsExport
24+
import kotlin.js.JsExport
25+
import kotlinx.coroutines.CancellationException as CommonCancellationException
26+
import kotlinx.coroutines.TimeoutCancellationException as CommonTimeoutCancellationException
27+
28+
@JsExport
29+
open class CancellationException(message: String? = null, cause: Throwable? = null) :
30+
IllegalStateException(message, cause) {
31+
override fun import() = CommonCancellationException(message, cause)
32+
}
33+
34+
fun CommonCancellationException.export() = CancellationException(message, cause)
35+
36+
// WARNING: as `coroutine` field and ctor are internal in kotlin implementation, we can't export it properly.
37+
// Current solution: ignore this field and import()
38+
@JsExport
39+
open class TimeoutCancellationException(message: String?) : CancellationException(message)
40+
41+
fun CommonTimeoutCancellationException.export() = TimeoutCancellationException(message)

samples/src/commonMain/kotlin/sample/_exception/ExceptionBuilder.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sample._exception
22

33
import deezer.kustomexport.KustomExport
4+
import kotlinx.coroutines.CancellationException
45

56
@KustomExport
67
class ExceptionBuilder {
@@ -20,6 +21,9 @@ class ExceptionBuilder {
2021
fun buildNumberFormatException(msg: String) = NumberFormatException(msg)
2122
fun buildRuntimeException(msg: String) = RuntimeException(msg)
2223
fun buildUnsupportedOperationException(msg: String) = UnsupportedOperationException(msg)
24+
25+
// Coroutines
26+
fun buildCancellationException(msg: String) = CancellationException(msg)
2327
}
2428

2529
@KustomExport
@@ -36,6 +40,8 @@ class ExceptionConsumer {
3640
fun consume(e: Exception): String {
3741
@Suppress("USELESS_IS_CHECK") // Cause typescript can go crazy
3842
return when (e) {
43+
is CancellationException -> "CancellationException=${e.message}"
44+
3945
is MyEx1 -> "MyEx1=${e.message}"
4046
is MyEx2 -> "MyEx2=${e.message}"
4147
is ArithmeticException -> "ArithmeticException=${e.message}"

samples/src/commonMain/kotlin/sample/_exception/ExceptionBuilder.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,6 @@ runTest("Exceptions", () : void => {
5858
Error, deezer.kustomexport.Exception, deezer.kustomexport.RuntimeException, deezer.kustomexport.NoSuchElementException)
5959
assertException(builder.buildArithmeticException("ae"), "ae",
6060
Error, deezer.kustomexport.Exception, deezer.kustomexport.RuntimeException, deezer.kustomexport.ArithmeticException)
61+
assertException(builder.buildCancellationException("cancel"), "cancel",
62+
Error, deezer.kustomexport.Exception, deezer.kustomexport.RuntimeException, deezer.kustomexport.IllegalStateException, deezer.kustomexport.CancellationException)
6163
})

0 commit comments

Comments
 (0)