diff --git a/core/common/src/main/java/com/puzzle/common/ResultUtil.kt b/core/common/src/main/java/com/puzzle/common/ResultUtil.kt new file mode 100644 index 0000000..58aff15 --- /dev/null +++ b/core/common/src/main/java/com/puzzle/common/ResultUtil.kt @@ -0,0 +1,13 @@ +package com.puzzle.common + +import kotlin.coroutines.cancellation.CancellationException + +suspend inline fun T.suspendRunCatching(crossinline block: suspend T.() -> R): Result { + return try { + Result.success(block()) + } catch (e: CancellationException) { + throw e + } catch (t: Throwable) { + Result.failure(t) + } +} \ No newline at end of file diff --git a/core/data/build.gradle.kts b/core/data/build.gradle.kts index e903b3c..5d2e8f3 100644 --- a/core/data/build.gradle.kts +++ b/core/data/build.gradle.kts @@ -11,4 +11,5 @@ dependencies { implementation(projects.core.domain) implementation(projects.core.network) implementation(projects.core.database) + implementation(projects.core.common) } diff --git a/core/data/src/main/java/com/puzzle/data/repository/TermsRepositoryImpl.kt b/core/data/src/main/java/com/puzzle/data/repository/TermsRepositoryImpl.kt index 225a74e..a824010 100644 --- a/core/data/src/main/java/com/puzzle/data/repository/TermsRepositoryImpl.kt +++ b/core/data/src/main/java/com/puzzle/data/repository/TermsRepositoryImpl.kt @@ -1,5 +1,6 @@ package com.puzzle.data.repository +import com.puzzle.common.suspendRunCatching import com.puzzle.database.model.terms.TermEntity import com.puzzle.database.source.term.LocalTermDataSource import com.puzzle.domain.model.terms.Term @@ -12,7 +13,7 @@ class TermsRepositoryImpl @Inject constructor( private val termDataSource: TermDataSource, private val localTermDataSource: LocalTermDataSource, ) : TermsRepository { - override suspend fun loadTerms(): Result = runCatching { + override suspend fun loadTerms(): Result = suspendRunCatching { val terms = termDataSource.loadTerms() .getOrThrow() .toDomain() @@ -31,8 +32,8 @@ class TermsRepositoryImpl @Inject constructor( localTermDataSource.clearAndInsertTerms(termsEntity) } - override suspend fun getTerms(): Result> = runCatching { + override suspend fun getTerms(): Result> = suspendRunCatching { localTermDataSource.getTerms() - .map { it.toDomain() } + .map(TermEntity::toDomain) } } diff --git a/core/network/src/main/java/com/puzzle/network/model/terms/LoadTermsResponse.kt b/core/network/src/main/java/com/puzzle/network/model/terms/LoadTermsResponse.kt index e2b6cfa..bb705cf 100644 --- a/core/network/src/main/java/com/puzzle/network/model/terms/LoadTermsResponse.kt +++ b/core/network/src/main/java/com/puzzle/network/model/terms/LoadTermsResponse.kt @@ -11,7 +11,7 @@ import java.time.LocalDateTime data class LoadTermsResponse( val responses: List?, ) { - fun toDomain() = responses?.map { it.toDomain() } ?: emptyList() + fun toDomain() = responses?.map(TermResponse::toDomain) ?: emptyList() } @Serializable