From 16c32d6b093624a2e81da7ff3334ac531d3a84b6 Mon Sep 17 00:00:00 2001 From: Scarlet Pan Date: Mon, 17 Nov 2025 08:53:59 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat=20try-catch=20v1.0.0=EF=BC=9A=201.?= =?UTF-8?q?=E4=BF=AE=E8=AE=A2Doc=E6=96=87=E6=A1=A3=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dev/scarlet/lang/coroutine/Catchers.kt | 50 +++++++++---------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/try-catch/src/main/java/dev/scarlet/lang/coroutine/Catchers.kt b/try-catch/src/main/java/dev/scarlet/lang/coroutine/Catchers.kt index 620c2aa..592a51d 100644 --- a/try-catch/src/main/java/dev/scarlet/lang/coroutine/Catchers.kt +++ b/try-catch/src/main/java/dev/scarlet/lang/coroutine/Catchers.kt @@ -49,20 +49,20 @@ fun Throwable.checkNonCancel() { * * Example: * ```kotlin - * suspend fun getUserData(): User = runCatching { - * service.getUserData() // suspending call that may throw IllegalStateException + * suspend fun fetchConfig(): Config = runCatching { + * remoteConfigService.getConfig() // suspending call that may throw IllegalStateException * } catchNonCancel { e: IllegalStateException -> - * Log.w(TAG, "Invalid user state, using default.", e) - * defaultUser + * Log.w(TAG, "Invalid remote config state, using default.", e) + * defaultConfig * } catchNonCancel { e -> - * Log.w(TAG, "Failed to load user, using default.", e) - * defaultUser + * Log.w(TAG, "Failed to fetch config, using default.", e) + * defaultConfig * } * ``` */ @JvmName("catchNonCancelFromResult") inline infix fun Result.catchNonCancel(transform: (e: IllegalStateException) -> T): Catcher = - Catcher.of(this).recoverCatching { e -> + Catcher.of(this).recoverCatching { e -> e.checkNonCancel() when (e) { is IllegalStateException -> transform(e) @@ -78,11 +78,11 @@ inline infix fun Result.catchNonCancel(transform: (e: IllegalStateExcepti * * Example: Safely fetch data in a coroutine with a fallback for non-cancellation exceptions * ```kotlin - * suspend fun getUserData(): User = runCatching { - * service.getUserData() + * suspend fun fetchConfig(): Config = runCatching { + * remoteConfigService.getConfig() * } catchNonCancel { e -> - * Log.w(TAG, "Failed to load user, using default.", e) - * defaultUser + * Log.w(TAG, "Failed to fetch config, using default.", e) + * defaultConfig * } * ``` * @@ -102,22 +102,22 @@ infix fun Result.catchNonCancel(transform: (e: Exception) -> T): T = * * Example: * ```kotlin - * suspend fun getUserData(): User = runCatching { - * service.getUserData() // suspending call that may throw IllegalStateException + * suspend fun fetchConfig(): Config = runCatching { + * remoteConfigService.getConfig() // suspending call that may throw IllegalStateException * } catch { e: IOException -> * throw e * } catchNonCancel { e: IllegalStateException -> - * Log.w(TAG, "Invalid user state, using default.", e) - * defaultUser + * Log.w(TAG, "Invalid remote config state, using default.", e) + * defaultConfig * } catchNonCancel { e -> - * Log.w(TAG, "Failed to load user, using default.", e) - * defaultUser + * Log.w(TAG, "Failed to fetch config, using default.", e) + * defaultConfig * } * ``` */ @JvmName("catchNonCancelIllegalStateFromCatcher") infix fun Catcher.catchNonCancel(transform: (e: IllegalStateException) -> T): Catcher = - recover { e -> + recover { e -> e.checkNonCancel() when (e) { is IllegalStateException -> transform(e) @@ -133,14 +133,14 @@ infix fun Catcher.catchNonCancel(transform: (e: IllegalStateException) -> * * Example: * ```kotlin - * suspend fun getUserData(): User = runCatching { - * service.getUserData() // suspending call that may throw IllegalStateException + * suspend fun fetchConfig(): Config = runCatching { + * remoteConfigService.getConfig() // suspending call that may throw IllegalStateException * } catchNonCancel { e: IllegalStateException -> - * Log.w(TAG, "Invalid user state.", e) - * defaultUser + * Log.w(TAG, "Invalid remote config state.", e) + * defaultConfig * } catchNonCancel { e -> - * Log.w(TAG, "Fail to get user.", e) - * defaultUser + * Log.w(TAG, "Fail to get config.", e) + * defaultConfig * } * ``` * @@ -174,7 +174,7 @@ infix fun Catcher.catchNonCancel(transform: (e: Exception) -> T): T = get @Deprecated(MESSAGE, replaceWith = ReplaceWith(EXPRESSION, imports = [IMPORT])) @JvmName("catchIllegalStateFromCatcher") inline infix fun Catcher.catch(transform: (e: IllegalStateException) -> T): Catcher = - recoverCatching { e -> + recoverCatching { e -> when (e) { is IllegalStateException -> transform(e) else -> throw e From a60897fd025fea7bb21145a19d1c91f10989811d Mon Sep 17 00:00:00 2001 From: Scarlet Pan Date: Mon, 17 Nov 2025 08:54:04 +0800 Subject: [PATCH 2/2] release try-catch v1.0.0 --- try-catch/build.gradle.kts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/try-catch/build.gradle.kts b/try-catch/build.gradle.kts index 58e6722..174d594 100644 --- a/try-catch/build.gradle.kts +++ b/try-catch/build.gradle.kts @@ -8,7 +8,7 @@ plugins { } group = "io.github.scarlet-pan" -version = "1.0.0-SNAPSHOT" +version = "1.0.0" java { sourceCompatibility = JavaVersion.VERSION_11