Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix-runcatching] suspend 함수에서 runCatching 사용하는 anti-pattern 수정 #405

Merged
merged 1 commit into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions core/util/src/main/java/com/ku_stacks/ku_ring/util/Result.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.ku_stacks.ku_ring.util

import kotlin.coroutines.cancellation.CancellationException

suspend inline fun <R> suspendRunCatching(block: () -> R): Result<R> {
return try {
Result.success(block())
} catch (c: CancellationException) {
throw c
} catch (e: Throwable) {
Result.failure(e)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.ku_stacks.ku_ring.local.room.DepartmentDao
import com.ku_stacks.ku_ring.preferences.PreferenceUtil
import com.ku_stacks.ku_ring.remote.department.DepartmentClient
import com.ku_stacks.ku_ring.util.IODispatcher
import com.ku_stacks.ku_ring.util.suspendRunCatching
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
Expand Down Expand Up @@ -40,7 +41,7 @@ class DepartmentRepositoryImpl @Inject constructor(
}

private suspend fun fetchDepartmentsFromRemote(): List<Department>? {
return runCatching {
return suspendRunCatching {
departmentClient.fetchDepartmentList().data?.map { it.toDepartment() } ?: emptyList()
}.getOrNull()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package com.ku_stacks.ku_ring.library.repository
import com.ku_stacks.ku_ring.domain.LibraryRoom
import com.ku_stacks.ku_ring.library.mapper.toLibraryAreaList
import com.ku_stacks.ku_ring.remote.library.LibraryClient
import com.ku_stacks.ku_ring.util.suspendRunCatching
import javax.inject.Inject

class LibraryRepositoryImpl @Inject constructor(
private val libraryClient: LibraryClient
) : LibraryRepository {
override suspend fun getRemainingSeats(): Result<List<LibraryRoom>> = runCatching {
override suspend fun getRemainingSeats(): Result<List<LibraryRoom>> = suspendRunCatching {
libraryClient.fetchRoomSeatStatus().toLibraryAreaList()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.ku_stacks.ku_ring.remote.notice.NoticeClient
import com.ku_stacks.ku_ring.remote.notice.request.SubscribeRequest
import com.ku_stacks.ku_ring.util.IODispatcher
import com.ku_stacks.ku_ring.util.WordConverter
import com.ku_stacks.ku_ring.util.suspendRunCatching
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
Expand Down Expand Up @@ -136,7 +137,7 @@ class NoticeRepositoryImpl @Inject constructor(
subscribeCategories: List<String>
) {
val subscribeRequest = SubscribeRequest(subscribeCategories)
runCatching {
suspendRunCatching {
noticeClient.saveSubscribe(
token,
subscribeRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.ku_stacks.ku_ring.preferences.PreferenceUtil
import com.ku_stacks.ku_ring.remote.notice.NoticeClient
import com.ku_stacks.ku_ring.remote.notice.response.NoticeResponse
import com.ku_stacks.ku_ring.util.DateUtil
import com.ku_stacks.ku_ring.util.suspendRunCatching

@OptIn(ExperimentalPagingApi::class)
class CategoryNoticeMediator(
Expand All @@ -36,7 +37,7 @@ class CategoryNoticeMediator(
return MediatorResult.Success(endOfPaginationReached = page != null)
}

return runCatching {
return suspendRunCatching {
val noticeResponse = noticeClient.fetchNoticeList(categoryShortName, page, itemSize)
insertNotices(noticeResponse.noticeResponse)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.ku_stacks.ku_ring.remote.user
import com.ku_stacks.ku_ring.remote.user.request.FeedbackRequest
import com.ku_stacks.ku_ring.remote.user.request.RegisterUserRequest
import com.ku_stacks.ku_ring.remote.util.DefaultResponse
import com.ku_stacks.ku_ring.util.suspendRunCatching
import javax.inject.Inject

class UserClient @Inject constructor(
Expand All @@ -22,7 +23,7 @@ class UserClient @Inject constructor(
userService.registerUser(RegisterUserRequest(token))

suspend fun getKuringBotQueryCount(token: String): Int {
return runCatching {
return suspendRunCatching {
userService.getKuringBotQueryCount(token).data.leftAskCount
}.getOrElse { 0 }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.ku_stacks.ku_ring.preferences.PreferenceUtil
import com.ku_stacks.ku_ring.remote.user.UserClient
import com.ku_stacks.ku_ring.remote.user.request.FeedbackRequest
import com.ku_stacks.ku_ring.remote.util.DefaultResponse
import com.ku_stacks.ku_ring.util.suspendRunCatching
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import javax.inject.Inject
Expand Down Expand Up @@ -33,7 +34,7 @@ class UserRepositoryImpl @Inject constructor(
}

override suspend fun sendFeedback(feedback: String): Result<DefaultResponse> {
return runCatching {
return suspendRunCatching {
userClient.sendFeedback(
token = pref.fcmToken,
feedbackRequest = FeedbackRequest(feedback)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.ku_stacks.ku_ring.thirdparty.firebase.analytics.EventAnalytics
import com.ku_stacks.ku_ring.util.WordConverter
import com.ku_stacks.ku_ring.util.modifyList
import com.ku_stacks.ku_ring.util.modifyMap
import com.ku_stacks.ku_ring.util.suspendRunCatching
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
Expand Down Expand Up @@ -83,7 +84,7 @@ class EditSubscriptionViewModel @Inject constructor(
private fun syncWithServer() {
fcmToken?.let {
viewModelScope.launch {
runCatching {
suspendRunCatching {
val subscribingList = noticeRepository.fetchSubscriptionFromRemote(it)
initialSortSubscription(subscribingList)
}.onFailure {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.ku_stacks.ku_ring.domain.WebViewNotice
import com.ku_stacks.ku_ring.notice.repository.NoticeRepository
import com.ku_stacks.ku_ring.util.suspendRunCatching
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
Expand Down Expand Up @@ -33,7 +34,7 @@ class NoticeWebViewModel @Inject constructor(

fun updateNoticeTobeRead(webViewNotice: WebViewNotice) {
viewModelScope.launch {
runCatching {
suspendRunCatching {
noticeRepository.updateNoticeToBeReadOnStorage(webViewNotice.articleId, webViewNotice.category)
noticeRepository.updateNoticeToBeRead(
webViewNotice.articleId,
Expand Down
Loading