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 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