Skip to content

Commit 4ffaace

Browse files
committed
fix: socket time out exception, unknown host excpetion 처리
1 parent 33137ae commit 4ffaace

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

data/src/main/java/com/stop/data/di/NetworkModule.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import okhttp3.Response
2020
import okhttp3.logging.HttpLoggingInterceptor
2121
import retrofit2.Retrofit
2222
import retrofit2.converter.moshi.MoshiConverterFactory
23+
import java.util.concurrent.TimeUnit
2324
import javax.inject.Named
2425
import javax.inject.Singleton
2526

@@ -40,6 +41,9 @@ internal object NetworkModule {
4041
loggingInterceptor: HttpLoggingInterceptor,
4142
): OkHttpClient {
4243
return OkHttpClient.Builder()
44+
.connectTimeout(500, TimeUnit.SECONDS)
45+
.writeTimeout(500, TimeUnit.SECONDS)
46+
.readTimeout(500, TimeUnit.SECONDS)
4347
.addInterceptor(loggingInterceptor)
4448
.addInterceptor(customInterceptor)
4549
.build()

presentation/src/main/java/com/stop/model/ErrorType.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,7 @@ enum class ErrorType(val stringResourcesId: Int) {
1212
AVAILABLE_BUS_NO_EXIST_YET(R.string.available_bus_no_exist_yet),
1313
BUS_DISAPPEAR_SUDDENLY(R.string.bus_disappear_suddenly),
1414
MISSION_SOMETHING_WRONG(R.string.mission_something_wrong),
15+
SOCKET_TIMEOUT_EXCEPTION(R.string.socket_timeout_exception_please_retry),
16+
UNKNOWN_EXCEPTION(R.string.unknown_exception_occur),
17+
UNKNOWN_HOST_EXCEPTION(R.string.unknown_host_exception_occur),
1518
}

presentation/src/main/java/com/stop/ui/route/RouteViewModel.kt

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ import com.stop.model.ErrorType
1414
import com.stop.model.Event
1515
import com.stop.model.route.Place
1616
import dagger.hilt.android.lifecycle.HiltViewModel
17+
import kotlinx.coroutines.CoroutineExceptionHandler
18+
import kotlinx.coroutines.Dispatchers
1719
import kotlinx.coroutines.launch
20+
import java.net.SocketTimeoutException
21+
import java.net.UnknownHostException
1822
import javax.inject.Inject
1923

2024
@HiltViewModel
@@ -50,6 +54,16 @@ class RouteViewModel @Inject constructor(
5054
val isLoading: LiveData<Event<Boolean>>
5155
get() = _isLoading
5256

57+
private val coroutineExceptionHandler = CoroutineExceptionHandler { _, throwable ->
58+
val errorMessage = when (throwable) {
59+
is SocketTimeoutException -> Event(ErrorType.SOCKET_TIMEOUT_EXCEPTION)
60+
is UnknownHostException -> Event(ErrorType.UNKNOWN_HOST_EXCEPTION)
61+
else -> Event(ErrorType.UNKNOWN_EXCEPTION)
62+
}
63+
_errorMessage.postValue(errorMessage)
64+
_isLoading.postValue(Event(false))
65+
}
66+
5367
fun patchRoute(isShowError: Boolean = true) {
5468
val originValue = _origin.value ?: let {
5569
if (!isShowError) {
@@ -75,7 +89,7 @@ class RouteViewModel @Inject constructor(
7589
endY = destinationValue.coordinate.latitude,
7690
)
7791

78-
viewModelScope.launch {
92+
viewModelScope.launch(Dispatchers.Default + coroutineExceptionHandler) {
7993
val itineraries = getRouteUseCase(routeRequest)
8094
if (itineraries.isEmpty()) {
8195
_errorMessage.value = Event(ErrorType.NO_ROUTE_RESULT)
@@ -97,8 +111,9 @@ class RouteViewModel @Inject constructor(
97111

98112
fun calculateLastTransportTime(itinerary: Itinerary) {
99113
checkClickedItinerary(itinerary)
100-
viewModelScope.launch {
101-
this@RouteViewModel._lastTimeResponse.value = Event(getLastTransportTimeUseCase(itinerary) )
114+
viewModelScope.launch(Dispatchers.Default + coroutineExceptionHandler) {
115+
this@RouteViewModel._lastTimeResponse.value =
116+
Event(getLastTransportTimeUseCase(itinerary))
102117
}
103118
}
104119

presentation/src/main/res/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,7 @@
8080
<string name="end_position_text">도착지 : %1$s</string>
8181
<string name="last_time_text">막차시간 : %1$s</string>
8282
<string name="walk_time_text">%1$d분</string>
83+
<string name="socket_timeout_exception_please_retry">API 서버가 응답하지 않습니다. 다시 시도해주세요.</string>
84+
<string name="unknown_exception_occur">알 수 없는 에러가 발생했습니다.</string>
85+
<string name="unknown_host_exception_occur">인터넷이 연결되어 있지 않거나, 페쇄망 호스트에 연결을 시도했습니다.</string>
8386
</resources>

0 commit comments

Comments
 (0)