-
Notifications
You must be signed in to change notification settings - Fork 0
[REFACTOR/#297] 에러 핸들링 중앙화 해버리기 #316
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
de0d166
[ADD/#297] 네트워크 연결 상태 Observer 추가
MoonsuKang 65122a7
[DEL#297] 네트워크 유틸리티 클래스 및 관련 모듈 삭제
MoonsuKang e83a7a8
[REFACTOR/#297] Diary UseCase 반환 타입 수정 및 추가
MoonsuKang 82cdd7f
[ADD/#297] ErrorMessageProvider 추가
MoonsuKang 43e3aee
[CHORE/#297] ErrorMessages 객체 삭제
MoonsuKang 0d1ddf3
[ADD/#297] 오류 메시지 문자열 추가
MoonsuKang 2adabf1
[REFACTOR/#297] 네트워크 유틸리티 및 에러 메시지 처리 방식 변경
MoonsuKang 91b35ac
[REFACTOR/#297] enableDraftAlarm 함수 인자 제거
MoonsuKang 89fe3c8
[REFACTOR/#297] 일부 feature을 실험용으로 SafeApiCall 도입
MoonsuKang c2d2e17
[CHORE/#297] conflict 해결
SYAAINN b48ca20
[CHORE/#297] ktlint Format
SYAAINN d71ab48
[CHORE/#297] CI 빌드 오류 해결
SYAAINN File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
app/src/main/java/com/sopt/clody/core/network/NetworkConnectivityModule.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package com.sopt.clody.core.network | ||
|
|
||
| import android.content.Context | ||
| import dagger.Module | ||
| import dagger.Provides | ||
| import dagger.hilt.InstallIn | ||
| import dagger.hilt.android.qualifiers.ApplicationContext | ||
| import dagger.hilt.components.SingletonComponent | ||
| import javax.inject.Singleton | ||
|
|
||
| @Module | ||
| @InstallIn(SingletonComponent::class) | ||
| object NetworkConnectivityModule { | ||
|
|
||
| @Provides | ||
| @Singleton | ||
| fun provideNetworkConnectivityObserver( | ||
| @ApplicationContext context: Context, | ||
| ): NetworkConnectivityObserver { | ||
| return NetworkConnectivityObserver(context) | ||
| } | ||
| } |
79 changes: 79 additions & 0 deletions
79
app/src/main/java/com/sopt/clody/core/network/NetworkConnectivityObserver.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package com.sopt.clody.core.network | ||
|
|
||
| import android.content.Context | ||
| import android.net.ConnectivityManager | ||
| import android.net.Network | ||
| import android.net.NetworkCapabilities | ||
| import android.net.NetworkRequest | ||
| import dagger.hilt.android.qualifiers.ApplicationContext | ||
| import kotlinx.coroutines.channels.awaitClose | ||
| import kotlinx.coroutines.flow.Flow | ||
| import kotlinx.coroutines.flow.callbackFlow | ||
| import kotlinx.coroutines.flow.distinctUntilChanged | ||
| import javax.inject.Inject | ||
|
|
||
| /** | ||
| * 네트워크 연결 상태를 관찰하는 Observer. | ||
| * | ||
| * - `Available`: 인터넷에 연결되어 있음 | ||
| * - `Unavailable`: 인터넷 연결이 끊긴 상태 | ||
| * | ||
| */ | ||
| class NetworkConnectivityObserver @Inject constructor( | ||
| @ApplicationContext private val context: Context, | ||
| ) { | ||
| private val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | ||
|
|
||
| /** | ||
| * 네트워크 상태를 실시간으로 스트리밍하는 Flow. | ||
| * | ||
| * - 최초 구독 시 현재 상태를 먼저 전송 -> | ||
| * - 이후 네트워크 변경 이벤트를 수신하여 상태를 전송 | ||
| * - 중복 상태 전송은 [distinctUntilChanged]로 방지 하도록 함. | ||
| */ | ||
| val networkStatus: Flow<NetworkStatus> = callbackFlow { | ||
| val callback = object : ConnectivityManager.NetworkCallback() { | ||
|
|
||
| /** | ||
| * 네트워크가 변경되었을 때 호출됨. | ||
| * 유효한 인터넷 연결이 있는지 확인하여 상태를 전송. | ||
| */ | ||
| override fun onCapabilitiesChanged(network: Network, capabilities: NetworkCapabilities) { | ||
| val hasInternet = capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) && | ||
| capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED) | ||
| trySend(if (hasInternet) NetworkStatus.Available else NetworkStatus.Unavailable) | ||
| } | ||
|
|
||
| /** | ||
| * 네트워크 연결이 완전히 끊겼을 때 호출. | ||
| */ | ||
| override fun onLost(network: Network) { | ||
| trySend(NetworkStatus.Unavailable) | ||
| } | ||
| } | ||
|
|
||
| trySend(if (isCurrentlyAvailable()) NetworkStatus.Available else NetworkStatus.Unavailable) | ||
|
|
||
| val request = NetworkRequest.Builder() | ||
| .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) | ||
| .build() | ||
|
|
||
| connectivityManager.registerNetworkCallback(request, callback) | ||
|
|
||
| awaitClose { | ||
| connectivityManager.unregisterNetworkCallback(callback) | ||
| } | ||
| }.distinctUntilChanged() | ||
|
|
||
| /** | ||
| * 현재 활성 네트워크가 인터넷에 연결되어 있는지를 반환 | ||
| * | ||
| * @return 인터넷 연결 여부 | ||
| */ | ||
| private fun isCurrentlyAvailable(): Boolean { | ||
| val network = connectivityManager.activeNetwork ?: return false | ||
| val capabilities = connectivityManager.getNetworkCapabilities(network) ?: return false | ||
| return capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) && | ||
| capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED) | ||
| } | ||
| } |
6 changes: 6 additions & 0 deletions
6
app/src/main/java/com/sopt/clody/core/network/NetworkStatus.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package com.sopt.clody.core.network | ||
|
|
||
| sealed class NetworkStatus { | ||
| data object Available : NetworkStatus() | ||
| data object Unavailable : NetworkStatus() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
app/src/main/java/com/sopt/clody/data/remote/util/ApiError.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package com.sopt.clody.data.remote.util | ||
|
|
||
| data class ApiError( | ||
| override val message: String, | ||
| ) : Exception() | ||
12 changes: 0 additions & 12 deletions
12
app/src/main/java/com/sopt/clody/data/remote/util/NetworkUtil.kt
This file was deleted.
Oops, something went wrong.
25 changes: 25 additions & 0 deletions
25
app/src/main/java/com/sopt/clody/data/remote/util/SafeApiCall.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package com.sopt.clody.data.remote.util | ||
|
|
||
| import com.sopt.clody.data.remote.dto.base.ApiResponse | ||
| import com.sopt.clody.presentation.utils.network.ErrorMessageProvider | ||
| import java.io.IOException | ||
| import kotlin.coroutines.cancellation.CancellationException | ||
|
|
||
| suspend fun <T> safeApiCall( | ||
| errorMessageProvider: ErrorMessageProvider, | ||
| action: suspend () -> ApiResponse<T>, | ||
| ): Result<T> { | ||
| return try { | ||
| val response = action() | ||
| response.data?.let { Result.success(it) } | ||
| ?: Result.failure(ApiError(errorMessageProvider.getTemporaryError())) | ||
| } catch (exception: Throwable) { | ||
| if (exception is CancellationException) throw exception | ||
|
|
||
| val error = when (exception) { | ||
| is IOException -> ApiError(errorMessageProvider.getNetworkError()) | ||
| else -> ApiError(errorMessageProvider.getTemporaryError()) | ||
| } | ||
| Result.failure(error) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ApiErrorwon’t compile – illegal override of nullablemessageException.messageis declared asString?. Overriding it with a non-nullableStringviolates the Liskov rule and the Kotlin compiler rejects it.This preserves the non-null guarantee for callers via
errorMessagewhile delegating to the baseExceptioncorrectly.🤖 Prompt for AI Agents