Skip to content

Commit

Permalink
v0.4.1 Expose CancellationException.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregory Lureau committed Feb 24, 2022
1 parent a08b944 commit dfeb43f
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ plugins {

allprojects {
group = "deezer.kustomexport"
version = "0.4.0"
version = "0.4.1"

repositories {
mavenLocal()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -58,4 +61,6 @@ val ALL_KOTLIN_EXCEPTIONS = listOf(
ASSERTION_ERROR,
NO_SUCH_ELEMENT_EXCEPTION,
ARITHMETIC_EXCEPTION,
CANCELLATION_EXCEPTION,
TIMEOUT_CANCELLATION_EXCEPTION
)
6 changes: 6 additions & 0 deletions lib-coroutines/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sample._exception

import deezer.kustomexport.KustomExport
import kotlinx.coroutines.CancellationException

@KustomExport
class ExceptionBuilder {
Expand All @@ -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
Expand All @@ -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}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

0 comments on commit dfeb43f

Please sign in to comment.