Skip to content

Commit ee1ec2c

Browse files
committed
[BOOK-145] refactor: handleException 함수 개선
1 parent 1823b96 commit ee1ec2c

File tree

6 files changed

+73
-18
lines changed

6 files changed

+73
-18
lines changed

core/common/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ android {
1414
dependencies {
1515
implementations(
1616
projects.core.model,
17+
projects.core.network,
1718

1819
libs.logger,
1920
)
Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,77 @@
11
package com.ninecraft.booket.core.common.utils
22

3+
import com.ninecraft.booket.core.network.response.ErrorResponse
34
import com.orhanobut.logger.Logger
5+
import kotlinx.serialization.SerializationException
6+
import kotlinx.serialization.json.Json
47
import retrofit2.HttpException
8+
import java.io.IOException
59
import java.net.ConnectException
610
import java.net.SocketTimeoutException
711
import java.net.UnknownHostException
812

913
fun handleException(
1014
exception: Throwable,
15+
onError: (String) -> Unit,
1116
onLoginRequired: () -> Unit,
12-
onServerError: (String) -> Unit,
13-
onNetworkError: (String) -> Unit,
1417
) {
1518
when {
1619
exception is HttpException && exception.code() == 401 -> {
1720
onLoginRequired()
1821
}
1922

20-
exception is HttpException && exception.code() in 500..599 -> {
21-
onServerError("서버 오류가 발생했습니다.")
23+
exception is HttpException -> {
24+
val serverMessage = exception.parseErrorMessage()
25+
val message = serverMessage ?: getHttpErrorMessage(exception.code())
26+
Logger.e("HTTP ${exception.code()}: $message")
27+
onError(message)
2228
}
2329

24-
exception is UnknownHostException || exception is ConnectException -> {
25-
onNetworkError("네트워크 연결을 확인해주세요.")
26-
}
27-
28-
exception is SocketTimeoutException -> {
29-
onServerError("서버 응답 시간이 초과되었습니다.")
30+
exception.isNetworkError() -> {
31+
onError("네트워크 연결을 확인해주세요.")
3032
}
3133

3234
else -> {
33-
val errorMessage = exception.message ?: "알 수 없는 오류가 발생했습니다."
35+
val errorMessage = exception.message ?: "알 수 없는 오류가 발생했습니다"
3436
Logger.e(errorMessage)
35-
onServerError(errorMessage)
37+
onError(errorMessage)
3638
}
3739
}
3840
}
41+
42+
@Suppress("TooGenericExceptionCaught")
43+
private fun HttpException.parseErrorMessage(): String? {
44+
return try {
45+
val errorBody = response()?.errorBody()?.string()
46+
if (errorBody.isNullOrBlank()) return null
47+
Json.decodeFromString<ErrorResponse>(errorBody).getErrorMessage()
48+
} catch (e: SerializationException) {
49+
Logger.e("JSON parsing failed: ${e.message}")
50+
null
51+
} catch (e: IOException) {
52+
Logger.e("Failed to read error body: ${e.message}")
53+
null
54+
} catch (e: Exception) {
55+
Logger.e("Unexpected error parsing response: ${e.message}")
56+
null
57+
}
58+
}
59+
60+
private fun getHttpErrorMessage(statusCode: Int): String {
61+
return when (statusCode) {
62+
400 -> "요청이 올바르지 않습니다"
63+
403 -> "접근 권한이 없습니다"
64+
404 -> "존재하지 않는 데이터입니다"
65+
429 -> "요청이 너무 많습니다. 잠시 후 다시 시도해주세요"
66+
in 400..499 -> "요청 처리 중 오류가 발생했습니다"
67+
in 500..599 -> "서버 오류가 발생했습니다"
68+
else -> "알 수 없는 오류가 발생했습니다"
69+
}
70+
}
71+
72+
private fun Throwable.isNetworkError(): Boolean {
73+
return this is UnknownHostException ||
74+
this is ConnectException ||
75+
this is SocketTimeoutException ||
76+
this is IOException
77+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.ninecraft.booket.core.network.response
2+
3+
import kotlinx.serialization.SerialName
4+
import kotlinx.serialization.Serializable
5+
6+
@Serializable
7+
data class ErrorResponse(
8+
@SerialName("status")
9+
val status: String,
10+
@SerialName("code")
11+
val code: String,
12+
@SerialName("message")
13+
val message: String,
14+
) {
15+
fun getErrorMessage(): String {
16+
return message.takeIf { it.isNotBlank() } ?: "알 수 없는 오류가 발생했습니다"
17+
}
18+
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ class LibraryPresenter @AssistedInject constructor(
5252

5353
handleException(
5454
exception = exception,
55-
onServerError = handleErrorMessage,
56-
onNetworkError = handleErrorMessage,
55+
onError = handleErrorMessage,
5756
onLoginRequired = {
5857
navigator.resetRoot(LoginScreen)
5958
},

feature/search/src/main/kotlin/com/ninecraft/booket/feature/search/SearchPresenter.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ class SearchPresenter @AssistedInject constructor(
107107

108108
handleException(
109109
exception = exception,
110-
onServerError = handleErrorMessage,
111-
onNetworkError = handleErrorMessage,
110+
onError = handleErrorMessage,
112111
onLoginRequired = {
113112
navigator.resetRoot(LoginScreen)
114113
},

feature/settings/src/main/kotlin/com/ninecraft/booket/feature/settings/SettingsPresenter.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ class SettingsPresenter @AssistedInject constructor(
8787

8888
handleException(
8989
exception = exception,
90-
onServerError = handleErrorMessage,
91-
onNetworkError = handleErrorMessage,
90+
onError = handleErrorMessage,
9291
onLoginRequired = {
9392
navigator.resetRoot(LoginScreen)
9493
},

0 commit comments

Comments
 (0)