Skip to content

Commit

Permalink
[Setting] Error Message 처리 고도화
Browse files Browse the repository at this point in the history
  • Loading branch information
ajou4095 committed Feb 4, 2024
1 parent 2a3acf0 commit 89ed9c5
Show file tree
Hide file tree
Showing 20 changed files with 95 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import kotlinx.coroutines.launch
abstract class BaseViewModel : ViewModel() {
val handler = CoroutineExceptionHandler { _, throwable ->
viewModelScope.launch {
_errorEvent.emit(ErrorEvent(throwable))
_errorEvent.emit(ErrorEvent.Client(throwable))
}
}

private val _errorEvent: MutableEventFlow<ErrorEvent> = MutableEventFlow()
protected val _errorEvent: MutableEventFlow<ErrorEvent> = MutableEventFlow()
val errorEvent: EventFlow<ErrorEvent> = _errorEvent.asEventFlow()

fun launch(block: suspend CoroutineScope.() -> Unit) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package ac.dnd.bookkeeping.android.presentation.common.base

data class ErrorEvent(
val throwable: Throwable
)
import ac.dnd.bookkeeping.android.domain.model.error.ServerException

sealed class ErrorEvent(
open val exception: Throwable
) {
data class Client(override val exception: Throwable) : ErrorEvent(exception)
data class InvalidRequest(override val exception: ServerException) : ErrorEvent(exception)
data class UnavailableServer(override val exception: Throwable) : ErrorEvent(exception)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ac.dnd.bookkeeping.android.presentation.common.util

import ac.dnd.bookkeeping.android.presentation.R
import ac.dnd.bookkeeping.android.presentation.common.base.BaseViewModel
import ac.dnd.bookkeeping.android.presentation.common.base.ErrorEvent
import ac.dnd.bookkeeping.android.presentation.common.util.coroutine.event.eventObserve
import ac.dnd.bookkeeping.android.presentation.common.view.DialogScreen
import androidx.compose.runtime.Composable
Expand All @@ -26,23 +27,48 @@ import kotlin.coroutines.EmptyCoroutineContext
fun ErrorObserver(
viewModel: BaseViewModel
) {
var isDialogShowing by remember { mutableStateOf(false) }
var _error: ErrorEvent? by remember { mutableStateOf(null) }
val error = _error

if (isDialogShowing) {
DialogScreen(
title = stringResource(id = R.string.error_dialog_title),
onDismissRequest = {
isDialogShowing = false
}
)
when (error) {
is ErrorEvent.Client -> {
DialogScreen(
title = stringResource(id = R.string.error_dialog_client_title),
message = stringResource(id = R.string.error_dialog_client_content),
onDismissRequest = {
_error = null
}
)
}

is ErrorEvent.InvalidRequest -> {
DialogScreen(
title = stringResource(id = R.string.error_dialog_invalid_request_title),
message = error.exception.message,
onDismissRequest = {
_error = null
}
)
}

is ErrorEvent.UnavailableServer -> {
DialogScreen(
title = stringResource(id = R.string.error_dialog_unavailable_server_title),
message = stringResource(id = R.string.error_dialog_unavailable_server_content),
onDismissRequest = {
_error = null
}
)
}

else -> Unit
}

LaunchedEffectWithLifecycle(viewModel.errorEvent) {
viewModel.errorEvent.eventObserve { event ->
Timber.d(event.throwable)
Sentry.captureException(event.throwable)

isDialogShowing = true
_error = event
Timber.d(event.exception)
Sentry.captureException(event.exception)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package ac.dnd.bookkeeping.android.presentation.ui.main.home.event.common.group

import ac.dnd.bookkeeping.android.domain.model.error.ServerException

sealed interface AddGroupEvent {
sealed interface AddGroup : AddGroupEvent {
data object Success : AddGroup
data class Failure(val exception: ServerException) : AddGroup
data class Error(val exception: Throwable) : AddGroup
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,6 @@ private fun AddGroupScreen(
onResult()
onDismissRequest()
}

is AddGroupEvent.AddGroup.Failure -> {
// TODO : ERROR
}

is AddGroupEvent.AddGroup.Error -> {
// TODO : ERROR
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ac.dnd.bookkeeping.android.presentation.ui.main.home.event.common.group
import ac.dnd.bookkeeping.android.domain.model.error.ServerException
import ac.dnd.bookkeeping.android.domain.usecase.feature.group.AddGroupUseCase
import ac.dnd.bookkeeping.android.presentation.common.base.BaseViewModel
import ac.dnd.bookkeeping.android.presentation.common.base.ErrorEvent
import ac.dnd.bookkeeping.android.presentation.common.util.coroutine.event.EventFlow
import ac.dnd.bookkeeping.android.presentation.common.util.coroutine.event.MutableEventFlow
import ac.dnd.bookkeeping.android.presentation.common.util.coroutine.event.asEventFlow
Expand Down Expand Up @@ -46,11 +47,11 @@ class AddGroupViewModel @Inject constructor(
_state.value = AddGroupState.Init
when (exception) {
is ServerException -> {
_event.emit(AddGroupEvent.AddGroup.Failure(exception))
_errorEvent.emit(ErrorEvent.InvalidRequest(exception))
}

else -> {
_event.emit(AddGroupEvent.AddGroup.Error(exception))
_errorEvent.emit(ErrorEvent.UnavailableServer(exception))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
package ac.dnd.bookkeeping.android.presentation.ui.main.home.event.common.relation

import ac.dnd.bookkeeping.android.domain.model.error.ServerException

sealed interface SearchRelationEvent {
sealed interface GetGroup : SearchRelationEvent {
data class Failure(val exception: ServerException) : GetGroup
data class Error(val exception: Throwable) : GetGroup
}
}
sealed interface SearchRelationEvent
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ import ac.dnd.bookkeeping.android.presentation.common.theme.Headline2
import ac.dnd.bookkeeping.android.presentation.common.theme.Headline3
import ac.dnd.bookkeeping.android.presentation.common.theme.Primary4
import ac.dnd.bookkeeping.android.presentation.common.theme.Shapes
import ac.dnd.bookkeeping.android.presentation.common.util.ErrorObserver
import ac.dnd.bookkeeping.android.presentation.common.util.LaunchedEffectWithLifecycle
import ac.dnd.bookkeeping.android.presentation.common.util.coroutine.event.EventFlow
import ac.dnd.bookkeeping.android.presentation.common.util.coroutine.event.MutableEventFlow
import ac.dnd.bookkeeping.android.presentation.common.util.coroutine.event.eventObserve
import ac.dnd.bookkeeping.android.presentation.common.view.BottomSheetScreen
import ac.dnd.bookkeeping.android.presentation.common.view.CustomTextField
import ac.dnd.bookkeeping.android.presentation.common.view.DialogScreen
import ac.dnd.bookkeeping.android.presentation.common.view.confirm.ConfirmButton
import ac.dnd.bookkeeping.android.presentation.common.view.confirm.ConfirmButtonProperties
import ac.dnd.bookkeeping.android.presentation.common.view.confirm.ConfirmButtonSize
Expand Down Expand Up @@ -55,6 +57,7 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
Expand All @@ -79,6 +82,17 @@ fun SearchRelationScreen(
groups = groups
)
}
ErrorObserver(viewModel)

var isDialogShowing by remember { mutableStateOf(false) }

DialogScreen(
isShowing = isDialogShowing,
title = stringResource(id = R.string.error_dialog_title),
onDismissRequest = {
isDialogShowing = false
}
)

SearchRelationScreen(
appState = appState,
Expand Down Expand Up @@ -117,18 +131,6 @@ private fun SearchRelationScreen(
// appState.navController.navigate(EditRelationConstant.ROUTE)
}

fun getGroup(event: SearchRelationEvent.GetGroup) {
when (event) {
is SearchRelationEvent.GetGroup.Failure -> {
// TODO : ERROR
}

is SearchRelationEvent.GetGroup.Error -> {
// TODO : ERROR
}
}
}

BottomSheetScreen(
onDismissRequest = onDismissRequest,
properties = BottomSheetDialogProperties(
Expand Down Expand Up @@ -228,9 +230,7 @@ private fun SearchRelationScreen(

LaunchedEffectWithLifecycle(event, handler) {
event.eventObserve { event ->
when (event) {
is SearchRelationEvent.GetGroup -> getGroup(event)
}

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ac.dnd.bookkeeping.android.domain.model.error.ServerException
import ac.dnd.bookkeeping.android.domain.model.feature.group.GroupWithRelation
import ac.dnd.bookkeeping.android.domain.usecase.feature.group.GetGroupListWithRelationUseCase
import ac.dnd.bookkeeping.android.presentation.common.base.BaseViewModel
import ac.dnd.bookkeeping.android.presentation.common.base.ErrorEvent
import ac.dnd.bookkeeping.android.presentation.common.util.coroutine.event.EventFlow
import ac.dnd.bookkeeping.android.presentation.common.util.coroutine.event.MutableEventFlow
import ac.dnd.bookkeeping.android.presentation.common.util.coroutine.event.asEventFlow
Expand Down Expand Up @@ -39,11 +40,11 @@ class SearchRelationViewModel @Inject constructor(
}.onFailure { exception ->
when (exception) {
is ServerException -> {
_event.emit(SearchRelationEvent.GetGroup.Failure(exception))
_errorEvent.emit(ErrorEvent.InvalidRequest(exception))
}

else -> {
_event.emit(SearchRelationEvent.GetGroup.Error(exception))
_errorEvent.emit(ErrorEvent.UnavailableServer(exception))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ac.dnd.bookkeeping.android.domain.model.error.ServerException
import ac.dnd.bookkeeping.android.domain.model.legacy.HistoryInfoLegacy
import ac.dnd.bookkeeping.android.domain.usecase.legacy.GetHistoryInfoUseCase
import ac.dnd.bookkeeping.android.presentation.common.base.BaseViewModel
import ac.dnd.bookkeeping.android.presentation.common.base.ErrorEvent
import ac.dnd.bookkeeping.android.presentation.common.util.coroutine.event.EventFlow
import ac.dnd.bookkeeping.android.presentation.common.util.coroutine.event.MutableEventFlow
import ac.dnd.bookkeeping.android.presentation.common.util.coroutine.event.asEventFlow
Expand Down Expand Up @@ -42,13 +43,13 @@ class HistoryViewModel @Inject constructor(
}
.onFailure { exception ->
when (exception) {
is ServerException -> _event.emit(
HistoryMainEvent.GetHistoryInfoMain.Failure(
exception
)
)
is ServerException -> {
_errorEvent.emit(ErrorEvent.InvalidRequest(exception))
}

else -> _event.emit(HistoryMainEvent.GetHistoryInfoMain.Error(exception))
else -> {
_errorEvent.emit(ErrorEvent.UnavailableServer(exception))
}
}
}
_state.value = HistoryMainState.Init
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
package ac.dnd.bookkeeping.android.presentation.ui.main.home.history.main

import ac.dnd.bookkeeping.android.domain.model.error.ServerException

sealed interface HistoryMainEvent {
sealed interface GetHistoryInfoMain : HistoryMainEvent {
data class Failure(val exception: ServerException) : GetHistoryInfoMain
data class Error(val exception: Throwable) : GetHistoryInfoMain
}
}
sealed interface HistoryMainEvent
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,7 @@ fun HistoryMainScreen(

LaunchedEffectWithLifecycle(event, handler) {
event.eventObserve { event ->
when (event) {
is HistoryMainEvent.GetHistoryInfoMain -> {

}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
package ac.dnd.bookkeeping.android.presentation.ui.main.home.history.main.detail

import ac.dnd.bookkeeping.android.domain.model.error.ServerException

sealed interface HistoryDetailEvent {
sealed interface GetHistoryRelationList : HistoryDetailEvent {
data class Failure(val exception: ServerException) : GetHistoryRelationList
data class Error(val exception: Throwable) : GetHistoryRelationList
}
}
sealed interface HistoryDetailEvent
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ac.dnd.bookkeeping.android.domain.model.error.ServerException
import ac.dnd.bookkeeping.android.domain.model.legacy.GroupLegacy
import ac.dnd.bookkeeping.android.domain.usecase.legacy.GetHistoryGroupListUseCase
import ac.dnd.bookkeeping.android.presentation.common.base.BaseViewModel
import ac.dnd.bookkeeping.android.presentation.common.base.ErrorEvent
import ac.dnd.bookkeeping.android.presentation.common.util.coroutine.event.EventFlow
import ac.dnd.bookkeeping.android.presentation.common.util.coroutine.event.MutableEventFlow
import ac.dnd.bookkeeping.android.presentation.common.util.coroutine.event.asEventFlow
Expand Down Expand Up @@ -57,11 +58,11 @@ class HistoryDetailViewModel @Inject constructor(
.onFailure { exception ->
when (exception) {
is ServerException -> {
_event.emit(HistoryDetailEvent.GetHistoryRelationList.Failure(exception))
_errorEvent.emit(ErrorEvent.InvalidRequest(exception))
}

else -> {
_event.emit(HistoryDetailEvent.GetHistoryRelationList.Error(exception))
_errorEvent.emit(ErrorEvent.UnavailableServer(exception))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,14 @@ class LoginMainViewModel @Inject constructor(
}
}

private fun submitError(error: Throwable) = launch {
when (error) {
private fun submitError(exception: Throwable) = launch {
when (exception) {
is ServerException -> {
_event.emit(LoginMainEvent.Login.Failure(error))
_event.emit(LoginMainEvent.Login.Failure(exception))
}

else -> {
_event.emit(LoginMainEvent.Login.Error(error))
_event.emit(LoginMainEvent.Login.Error(exception))
}
}
_state.emit(LoginMainState.Init)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ac.dnd.bookkeeping.android.domain.model.error.ServerException
import ac.dnd.bookkeeping.android.domain.usecase.authentication.RegistrationUseCase
import ac.dnd.bookkeeping.android.domain.usecase.member.CheckNicknameUseCase
import ac.dnd.bookkeeping.android.presentation.common.base.BaseViewModel
import ac.dnd.bookkeeping.android.presentation.common.base.ErrorEvent
import ac.dnd.bookkeeping.android.presentation.common.util.coroutine.event.EventFlow
import ac.dnd.bookkeeping.android.presentation.common.util.coroutine.event.MutableEventFlow
import ac.dnd.bookkeeping.android.presentation.common.util.coroutine.event.asEventFlow
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ sealed interface SplashEvent {
sealed interface Login : SplashEvent {
data object Success : Login
data class Failure(val exception: ServerException) : Login
data class Error(val exception: Throwable) : Login
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ fun SplashScreen(
is SplashEvent.Login.Failure -> {
navigateToLogin()
}

is SplashEvent.Login.Error -> {
// TODO : Unknown Error (Client Error, Internal Server Error, ...)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ac.dnd.bookkeeping.android.presentation.ui.main.splash
import ac.dnd.bookkeeping.android.domain.model.error.ServerException
import ac.dnd.bookkeeping.android.domain.usecase.authentication.UpdateJwtTokenUseCase
import ac.dnd.bookkeeping.android.presentation.common.base.BaseViewModel
import ac.dnd.bookkeeping.android.presentation.common.base.ErrorEvent
import ac.dnd.bookkeeping.android.presentation.common.util.coroutine.event.EventFlow
import ac.dnd.bookkeeping.android.presentation.common.util.coroutine.event.MutableEventFlow
import ac.dnd.bookkeeping.android.presentation.common.util.coroutine.event.asEventFlow
Expand Down Expand Up @@ -49,7 +50,7 @@ class SplashViewModel @Inject constructor(
}

else -> {
_event.emit(SplashEvent.Login.Error(exception))
_errorEvent.emit(ErrorEvent.UnavailableServer(exception))
}
}
}
Expand Down
Loading

0 comments on commit 89ed9c5

Please sign in to comment.