From dfeb43f45c2ae4cbd272144e06f58ae75ab2ee87 Mon Sep 17 00:00:00 2001 From: Gregory Lureau Date: Thu, 24 Feb 2022 16:36:13 +0100 Subject: [PATCH] v0.4.1 Expose CancellationException. --- build.gradle.kts | 2 +- .../kustomexport/compiler/js/Exceptions.kt | 5 +++ lib-coroutines/build.gradle.kts | 6 +++ .../kotlin/deezer/kustomexport/Exceptions.kt | 41 +++++++++++++++++++ .../sample/_exception/ExceptionBuilder.kt | 6 +++ .../sample/_exception/ExceptionBuilder.ts | 2 + 6 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 lib-coroutines/src/commonMain/kotlin/deezer/kustomexport/Exceptions.kt diff --git a/build.gradle.kts b/build.gradle.kts index 07b3bb5..a9d9fec 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -24,7 +24,7 @@ plugins { allprojects { group = "deezer.kustomexport" - version = "0.4.0" + version = "0.4.1" repositories { mavenLocal() diff --git a/compiler/src/main/kotlin/deezer/kustomexport/compiler/js/Exceptions.kt b/compiler/src/main/kotlin/deezer/kustomexport/compiler/js/Exceptions.kt index 50c7c30..dea1440 100644 --- a/compiler/src/main/kotlin/deezer/kustomexport/compiler/js/Exceptions.kt +++ b/compiler/src/main/kotlin/deezer/kustomexport/compiler/js/Exceptions.kt @@ -37,6 +37,9 @@ val CLASS_CAST_EXCEPTION = ClassName("kotlin", "ClassCastException") val ASSERTION_ERROR = ClassName("kotlin", "AssertionError") val NO_SUCH_ELEMENT_EXCEPTION = ClassName("kotlin", "NoSuchElementException") val ARITHMETIC_EXCEPTION = ClassName("kotlin", "ArithmeticException") +// Coroutines +val CANCELLATION_EXCEPTION = ClassName("kotlinx.coroutines", "CancellationException") +val TIMEOUT_CANCELLATION_EXCEPTION = ClassName("kotlinx.coroutines", "TimeoutCancellationException") const val EXCEPTION_JS_PACKAGE = "deezer.kustomexport" fun TypeName.toJsException(): ClassName = ClassName(EXCEPTION_JS_PACKAGE, (this as ClassName).simpleName) @@ -58,4 +61,6 @@ val ALL_KOTLIN_EXCEPTIONS = listOf( ASSERTION_ERROR, NO_SUCH_ELEMENT_EXCEPTION, ARITHMETIC_EXCEPTION, + CANCELLATION_EXCEPTION, + TIMEOUT_CANCELLATION_EXCEPTION ) diff --git a/lib-coroutines/build.gradle.kts b/lib-coroutines/build.gradle.kts index 58f6a1f..64fb6cb 100644 --- a/lib-coroutines/build.gradle.kts +++ b/lib-coroutines/build.gradle.kts @@ -17,6 +17,12 @@ kotlin { languageSettings.optIn("kotlin.RequiresOptIn") languageSettings.optIn("kotlin.js.ExperimentalJsExport") } + val commonMain by getting { + dependencies { + implementation(project(":lib")) + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0") + } + } } targets.all { diff --git a/lib-coroutines/src/commonMain/kotlin/deezer/kustomexport/Exceptions.kt b/lib-coroutines/src/commonMain/kotlin/deezer/kustomexport/Exceptions.kt new file mode 100644 index 0000000..1b06bb8 --- /dev/null +++ b/lib-coroutines/src/commonMain/kotlin/deezer/kustomexport/Exceptions.kt @@ -0,0 +1,41 @@ +/* + * Copyright 2022 Deezer. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +@file:Suppress("NON_EXPORTABLE_TYPE") +@file:OptIn(ExperimentalJsExport::class) + +package deezer.kustomexport + +import kotlin.js.ExperimentalJsExport +import kotlin.js.JsExport +import kotlinx.coroutines.CancellationException as CommonCancellationException +import kotlinx.coroutines.TimeoutCancellationException as CommonTimeoutCancellationException + +@JsExport +open class CancellationException(message: String? = null, cause: Throwable? = null) : + IllegalStateException(message, cause) { + override fun import() = CommonCancellationException(message, cause) +} + +fun CommonCancellationException.export() = CancellationException(message, cause) + +// WARNING: as `coroutine` field and ctor are internal in kotlin implementation, we can't export it properly. +// Current solution: ignore this field and import() +@JsExport +open class TimeoutCancellationException(message: String?) : CancellationException(message) + +fun CommonTimeoutCancellationException.export() = TimeoutCancellationException(message) diff --git a/samples/src/commonMain/kotlin/sample/_exception/ExceptionBuilder.kt b/samples/src/commonMain/kotlin/sample/_exception/ExceptionBuilder.kt index a510a76..7d43bc1 100644 --- a/samples/src/commonMain/kotlin/sample/_exception/ExceptionBuilder.kt +++ b/samples/src/commonMain/kotlin/sample/_exception/ExceptionBuilder.kt @@ -1,6 +1,7 @@ package sample._exception import deezer.kustomexport.KustomExport +import kotlinx.coroutines.CancellationException @KustomExport class ExceptionBuilder { @@ -20,6 +21,9 @@ class ExceptionBuilder { fun buildNumberFormatException(msg: String) = NumberFormatException(msg) fun buildRuntimeException(msg: String) = RuntimeException(msg) fun buildUnsupportedOperationException(msg: String) = UnsupportedOperationException(msg) + + // Coroutines + fun buildCancellationException(msg: String) = CancellationException(msg) } @KustomExport @@ -36,6 +40,8 @@ class ExceptionConsumer { fun consume(e: Exception): String { @Suppress("USELESS_IS_CHECK") // Cause typescript can go crazy return when (e) { + is CancellationException -> "CancellationException=${e.message}" + is MyEx1 -> "MyEx1=${e.message}" is MyEx2 -> "MyEx2=${e.message}" is ArithmeticException -> "ArithmeticException=${e.message}" diff --git a/samples/src/commonMain/kotlin/sample/_exception/ExceptionBuilder.ts b/samples/src/commonMain/kotlin/sample/_exception/ExceptionBuilder.ts index 7459dae..1bbb57c 100644 --- a/samples/src/commonMain/kotlin/sample/_exception/ExceptionBuilder.ts +++ b/samples/src/commonMain/kotlin/sample/_exception/ExceptionBuilder.ts @@ -58,4 +58,6 @@ runTest("Exceptions", () : void => { Error, deezer.kustomexport.Exception, deezer.kustomexport.RuntimeException, deezer.kustomexport.NoSuchElementException) assertException(builder.buildArithmeticException("ae"), "ae", Error, deezer.kustomexport.Exception, deezer.kustomexport.RuntimeException, deezer.kustomexport.ArithmeticException) + assertException(builder.buildCancellationException("cancel"), "cancel", + Error, deezer.kustomexport.Exception, deezer.kustomexport.RuntimeException, deezer.kustomexport.IllegalStateException, deezer.kustomexport.CancellationException) }) \ No newline at end of file