Skip to content

Commit

Permalink
이미 종료된 채팅에 대한 결과 확인 임시 에러 처리
Browse files Browse the repository at this point in the history
TODO 에러 처리 로직을 제대로 분기 처리하여 구현해야 함
  • Loading branch information
easyhooon committed Nov 15, 2023
1 parent 16feb5b commit 5909835
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@ package com.last.psychat.android.core.data.datasource.remote.chat
import com.last.psychat.android.core.data.model.chat.ChatRequest
import com.last.psychat.android.core.data.model.chat.ChatResponse
import com.last.psychat.android.core.data.model.chat.CheckEmotionIsJudgeResponse
import com.last.psychat.android.core.data.model.chat.EmotionResponse
import com.last.psychat.android.core.data.model.chat.EndChatRequest
import com.last.psychat.android.core.data.model.chat.PreviousChatDetailResponse
import com.last.psychat.android.core.data.model.chat.PreviousChatListResponse
import com.last.psychat.android.core.data.model.chat.SessionResponse
import com.last.psychat.android.core.data.model.chat.EmotionResponse
import com.last.psychat.android.core.data.model.chat.PreviousChatDetailResponse
import com.last.psychat.android.core.data.service.ChatService
import com.last.psychat.android.core.data.util.ExceptionWrapper
import com.last.psychat.android.core.data.util.extension.safeRequest
import com.last.psychat.android.core.data.util.extension.toAlertMessage
import com.last.psychat.android.core.domain.util.EndChatSessionResponseServerError
import java.net.UnknownHostException
import javax.inject.Inject
import retrofit2.HttpException
import timber.log.Timber

internal class ChatRemoteDataSourceImpl @Inject constructor(
private val service: ChatService,
Expand Down Expand Up @@ -46,8 +52,45 @@ internal class ChatRemoteDataSourceImpl @Inject constructor(
}

override suspend fun endChatSession(endChatRequest: EndChatRequest): EmotionResponse? {
return safeRequest {
service.endChatSession(endChatRequest)
// return safeRequest {
// service.endChatSession(endChatRequest)
// }
try {
val response = service.endChatSession(endChatRequest)
if (response.isSuccessful) {
return response.body()
} else {
if (response.code() == 500) {
throw EndChatSessionResponseServerError
} else {
val errorBody = response.errorBody()?.string() ?: "Unknown error"
Timber.d(Exception(errorBody))
throw ExceptionWrapper(
statusCode = response.code(),
message = Exception(errorBody).toAlertMessage(),
cause = Exception(errorBody),
)
}
}
} catch (exception: HttpException) {
Timber.d(exception)
throw ExceptionWrapper(
statusCode = exception.code(),
message = exception.response()?.errorBody()?.string() ?: exception.message(),
cause = exception,
)
} catch (exception: UnknownHostException) {
Timber.d(exception)
throw ExceptionWrapper(
message = exception.toAlertMessage(),
cause = exception,
)
} catch (exception: Exception) {
Timber.d(exception)
throw ExceptionWrapper(
message = exception.toAlertMessage(),
cause = exception,
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ val PreviousChatListResponseIsNull = IOException("Get Previous ChatList API resp
val StartChatSessionResponseIsNull = IOException("Start Chat Session API response is null.")
val SendChatMessageResponseIsNull = IOException("Send Chat Message API response is null.")
val CheckEmotionIsJudgedResponseIsNull = IOException("Check Emotion is Judged API response is null.")
val EndChatSessionResponseServerError = IOException("End Chat Session API response is server error.")

// Recommend
val GetRecommendedContentListResponseIsNull = IOException("Get Recommended Content List API response is null.")
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.last.psychat.android.core.domain.usecase.chat.CheckEmotionIsJudgedUse
import com.last.psychat.android.core.domain.usecase.chat.EndChatSessionUseCase
import com.last.psychat.android.core.domain.usecase.chat.GetPreviousChatDetailUseCase
import com.last.psychat.android.core.domain.usecase.chat.SendChatMessageUseCase
import com.last.psychat.android.core.domain.util.EndChatSessionResponseServerError
import com.last.psychat.android.core.ui.UiText
import com.last.psychat.android.feature.chat.mapper.toUiModel
import com.last.psychat.android.feature.chat.model.ChatMessageUiModel
Expand Down Expand Up @@ -200,7 +201,12 @@ class ChatViewModel @Inject constructor(
}
result.isFailure -> {
val exception = result.exceptionOrNull()!!
_eventFlow.emit(ChatUiEvent.ShowToast(UiText.DirectString(exception.message.toString())))
if (exception == EndChatSessionResponseServerError) {
_eventFlow.emit(ChatUiEvent.ShowToast(UiText.StringResource(R.string.already_end_chat_session)))
} else {
_eventFlow.emit(ChatUiEvent.ShowToast(UiText.DirectString(exception.message.toString())))
// _eventFlow.emit(ChatUiEvent.ShowToast(UiText.StringResource(R.string.already_end_chat_session)))
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions feature/chat/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
<string name="start_chat_info">대화가 시작 되었습니다.\n감정을 더 정확하게 이해하기 위해, 많은 대화를 나누어 주세요.</string>
<string name="emotion_judgement_fail">아직 감정을 판단할 수 없어요. 좀 더 대화를 나눠 주세요.</string>
<string name="send_message_description">Send Message</string>
<string name="already_end_chat_session">이미 종료된 채팅 입니다.</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class MainViewModel @Inject constructor(
_eventFlow.emit(
MainUiEvent.NavigateToChat(
sessionId = sessionId,
isEndChat = true,
isEndChat = false,
),
)
}
Expand Down

0 comments on commit 5909835

Please sign in to comment.