Skip to content

Commit 49a1eaa

Browse files
authored
Merge pull request #196 from YAPP-Github/BOOK-367-feature/#195
fix: 로그인 관련 API 에러 처리 방식 개선
2 parents 207b101 + 61910e2 commit 49a1eaa

File tree

8 files changed

+64
-42
lines changed

8 files changed

+64
-42
lines changed

core/common/src/main/kotlin/com/ninecraft/booket/core/common/constants/ErrorDialogSpec.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.ninecraft.booket.core.common.constants
33
import androidx.annotation.StringRes
44

55
data class ErrorDialogSpec(
6+
val title: String? = null,
67
val message: String,
78
@StringRes val buttonLabelResId: Int,
89
val action: () -> Unit,
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package com.ninecraft.booket.core.common.constants
22

33
enum class ErrorScope {
4-
GLOBAL, LOGIN, BOOK_REGISTER, RECORD_REGISTER
4+
GLOBAL, LOGIN, AUTH_SESSION_EXPIRED,
55
}

core/common/src/main/kotlin/com/ninecraft/booket/core/common/utils/HandleException.kt

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ fun handleException(
2323
) {
2424
when {
2525
exception is HttpException && exception.code() == 401 -> {
26-
onLoginRequired()
26+
postErrorDialog(
27+
errorScope = ErrorScope.AUTH_SESSION_EXPIRED,
28+
exception = exception,
29+
action = {
30+
onLoginRequired()
31+
},
32+
)
2733
}
2834

2935
exception is HttpException -> {
@@ -51,53 +57,40 @@ fun postErrorDialog(
5157
@StringRes buttonLabelResId: Int = R.string.confirm,
5258
action: () -> Unit = {},
5359
) {
54-
val spec = buildDialog(
55-
scope = errorScope,
56-
exception = exception,
57-
buttonLabelResId = buttonLabelResId,
58-
action = action,
59-
)
60-
61-
ErrorEventHelper.sendError(event = ErrorEvent.ShowDialog(spec))
62-
}
63-
64-
private fun buildDialog(
65-
scope: ErrorScope,
66-
exception: Throwable,
67-
@StringRes buttonLabelResId: Int,
68-
action: () -> Unit,
69-
): ErrorDialogSpec {
70-
val message = when {
60+
val (title, message) = when {
7161
exception.isNetworkError() -> {
72-
"네트워크 연결이 불안정합니다.\n인터넷 연결을 확인해주세요"
62+
null to "네트워크 연결이 불안정합니다.\n인터넷 연결을 확인해주세요"
7363
}
7464

7565
exception is HttpException -> {
76-
when (scope) {
66+
when (errorScope) {
7767
ErrorScope.GLOBAL -> {
78-
"알 수 없는 문제가 발생했어요.\n다시 시도해주세요"
68+
null to "알 수 없는 문제가 발생했어요.\n다시 시도해주세요"
7969
}
8070

8171
ErrorScope.LOGIN -> {
82-
"예기치 않은 오류가 발생했습니다.\n다시 로그인 해주세요."
72+
"로그인 오류" to "예기치 않은 오류가 발생했습니다.\n다시 로그인 해주세요."
8373
}
8474

85-
ErrorScope.BOOK_REGISTER -> {
86-
"도서 등록 중 오류가 발생했어요.\n다시 시도해주세요"
87-
}
88-
89-
ErrorScope.RECORD_REGISTER -> {
90-
"기록 저장에 실패했어요.\n다시 시도해주세요"
75+
ErrorScope.AUTH_SESSION_EXPIRED -> {
76+
null to "세션이 만료되었어요.\n다시 로그인 해주세요"
9177
}
9278
}
9379
}
9480

9581
else -> {
96-
"알 수 없는 문제가 발생했어요.\n다시 시도해주세요"
82+
null to "알 수 없는 문제가 발생했어요.\n다시 시도해주세요"
9783
}
9884
}
9985

100-
return ErrorDialogSpec(message = message, buttonLabelResId = buttonLabelResId, action = action)
86+
val spec = ErrorDialogSpec(
87+
title = title,
88+
message = message,
89+
buttonLabelResId = buttonLabelResId,
90+
action = action,
91+
)
92+
93+
ErrorEventHelper.sendError(event = ErrorEvent.ShowDialog(spec))
10194
}
10295

10396
@Suppress("TooGenericExceptionCaught")

feature/home/src/main/kotlin/com/ninecraft/booket/feature/home/HomePresenter.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ import androidx.compose.runtime.mutableStateOf
66
import androidx.compose.runtime.rememberCoroutineScope
77
import androidx.compose.runtime.setValue
88
import com.ninecraft.booket.core.common.analytics.AnalyticsHelper
9+
import com.ninecraft.booket.core.common.utils.handleException
910
import com.ninecraft.booket.core.data.api.repository.AuthRepository
1011
import com.ninecraft.booket.core.data.api.repository.BookRepository
1112
import com.ninecraft.booket.core.model.RecentBookModel
1213
import com.ninecraft.booket.core.model.UserState
1314
import com.ninecraft.booket.feature.screens.BookDetailScreen
1415
import com.ninecraft.booket.feature.screens.BookSearchScreen
1516
import com.ninecraft.booket.feature.screens.HomeScreen
17+
import com.ninecraft.booket.feature.screens.LoginScreen
1618
import com.ninecraft.booket.feature.screens.RecordScreen
1719
import com.ninecraft.booket.feature.screens.SettingsScreen
1820
import com.skydoves.compose.effects.RememberedEffect
@@ -56,6 +58,14 @@ class HomePresenter @AssistedInject constructor(
5658
recentBooks = result.recentBooks.toPersistentList()
5759
}.onFailure { exception ->
5860
uiState = UiState.Error(exception)
61+
62+
handleException(
63+
exception = exception,
64+
onError = {},
65+
onLoginRequired = {
66+
navigator.resetRoot(LoginScreen())
67+
},
68+
)
5969
}
6070
}
6171
}

feature/library/src/main/kotlin/com/ninecraft/booket/feature/library/LibraryPresenter.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import androidx.compose.runtime.rememberCoroutineScope
88
import androidx.compose.runtime.setValue
99
import com.ninecraft.booket.core.common.analytics.AnalyticsHelper
1010
import com.ninecraft.booket.core.common.utils.UiText
11+
import com.ninecraft.booket.core.common.utils.handleException
1112
import com.ninecraft.booket.core.data.api.repository.AuthRepository
1213
import com.ninecraft.booket.core.data.api.repository.BookRepository
1314
import com.ninecraft.booket.core.model.LibraryBookSummaryModel
@@ -16,6 +17,7 @@ import com.ninecraft.booket.core.ui.component.FooterState
1617
import com.ninecraft.booket.feature.screens.BookDetailScreen
1718
import com.ninecraft.booket.feature.screens.LibraryScreen
1819
import com.ninecraft.booket.feature.screens.LibrarySearchScreen
20+
import com.ninecraft.booket.feature.screens.LoginScreen
1921
import com.ninecraft.booket.feature.screens.SettingsScreen
2022
import com.ninecraft.booket.feature.screens.extensions.redirectToLogin
2123
import com.orhanobut.logger.Logger
@@ -105,6 +107,14 @@ class LibraryPresenter @AssistedInject constructor(
105107
} else {
106108
footerState = FooterState.Error(errorMessage)
107109
}
110+
111+
handleException(
112+
exception = exception,
113+
onError = {},
114+
onLoginRequired = {
115+
navigator.resetRoot(LoginScreen())
116+
},
117+
)
108118
}
109119
}
110120
}
@@ -136,7 +146,10 @@ class LibraryPresenter @AssistedInject constructor(
136146
}
137147

138148
currentFilter = event.filterOption
139-
filterLibraryBooks(status = currentFilter.getApiValue(), page = START_INDEX, size = PAGE_SIZE)
149+
150+
if (userState !is UserState.Guest) {
151+
filterLibraryBooks(status = currentFilter.getApiValue(), page = START_INDEX, size = PAGE_SIZE)
152+
}
140153
}
141154

142155
is LibraryUiEvent.OnBookClick -> {

feature/login/src/main/kotlin/com/ninecraft/booket/feature/login/LoginPresenter.kt

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import androidx.compose.runtime.mutableStateOf
66
import androidx.compose.runtime.rememberCoroutineScope
77
import androidx.compose.runtime.setValue
88
import com.ninecraft.booket.core.common.analytics.AnalyticsHelper
9+
import com.ninecraft.booket.core.common.constants.ErrorScope
10+
import com.ninecraft.booket.core.common.utils.postErrorDialog
911
import com.ninecraft.booket.core.data.api.repository.AuthRepository
1012
import com.ninecraft.booket.core.data.api.repository.UserRepository
1113
import com.ninecraft.booket.feature.screens.HomeScreen
@@ -60,10 +62,11 @@ class LoginPresenter @AssistedInject constructor(
6062
}
6163
}
6264
}.onFailure { exception ->
63-
exception.message?.let { Logger.e(it) }
64-
sideEffect = exception.message?.let {
65-
LoginSideEffect.ShowToast(it)
66-
}
65+
Logger.e(exception.message ?: "Failed to get user profile")
66+
postErrorDialog(
67+
errorScope = ErrorScope.LOGIN,
68+
exception = exception,
69+
)
6770
}
6871
}
6972
}
@@ -72,7 +75,7 @@ class LoginPresenter @AssistedInject constructor(
7275
when (event) {
7376
is LoginUiEvent.OnKakaoLoginButtonClick -> {
7477
isLoading = true
75-
sideEffect = LoginSideEffect.KakaoLogin
78+
sideEffect = LoginSideEffect.KakaoLogin()
7679
}
7780

7881
is LoginUiEvent.LoginFailure -> {
@@ -89,11 +92,12 @@ class LoginPresenter @AssistedInject constructor(
8992
.onSuccess {
9093
navigateAfterLogin()
9194
}.onFailure { exception ->
92-
exception.message?.let { Logger.e(it) }
95+
Logger.e(exception.message ?: "Login failed")
9396
analyticsHelper.logEvent(EVENT_ERROR_LOGIN)
94-
sideEffect = exception.message?.let {
95-
LoginSideEffect.ShowToast(it)
96-
}
97+
postErrorDialog(
98+
errorScope = ErrorScope.LOGIN,
99+
exception = exception,
100+
)
97101
}
98102
} finally {
99103
isLoading = false

feature/login/src/main/kotlin/com/ninecraft/booket/feature/login/LoginUiState.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ data class LoginUiState(
1515

1616
@Immutable
1717
sealed interface LoginSideEffect {
18-
data object KakaoLogin : LoginSideEffect
18+
data class KakaoLogin(private val key: String = UUID.randomUUID().toString()) : LoginSideEffect
1919
data class ShowToast(
2020
val message: String,
2121
private val key: String = UUID.randomUUID().toString(),

feature/main/src/main/kotlin/com/ninecraft/booket/feature/main/MainActivity.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class MainActivity : ComponentActivity() {
7070

7171
dialogSpec.value?.let { spec ->
7272
ReedDialog(
73+
title = spec.title,
7374
description = spec.message,
7475
confirmButtonText = stringResource(spec.buttonLabelResId),
7576

0 commit comments

Comments
 (0)