Skip to content

Commit

Permalink
feat/#8: 네트워크 결과 처리 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
kangyein9892 committed Nov 15, 2024
1 parent a7005a4 commit f704238
Showing 1 changed file with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.sopt.and.data.common

import org.sopt.and.domain.exception.*
import retrofit2.HttpException
import java.net.ConnectException
import java.net.SocketTimeoutException
import java.net.UnknownHostException
import org.json.JSONObject
import org.json.JSONException

inline fun <T> execute(block: () -> T): Result<T> = runCatching {
Result.Success(block())
}.getOrElse {
Result.Error(handleNetworkError(it))
}

fun handleNetworkError(e: Throwable): NetworkError {
return when (e) {
is ConnectException -> ConnectError
is SocketTimeoutException -> TimeoutError
is UnknownHostException -> UnknownHostError
is HttpException -> mapHttpError(e)
else -> UnknownError
}
}

fun mapHttpError(e: HttpException): NetworkError {
return try {
val statusCode = e.code().toString()
val errorBody = e.response()?.errorBody()?.string()
val errorCode = extractErrorCode(errorBody).toString()
HttpCodeError(statusCode, errorCode)
} catch (ex: Exception) {
UnknownError
}
}

fun extractErrorCode(errorBody: String?): String? {
return try {
errorBody?.let { JSONObject(it).getString("code") }
} catch (e: JSONException) {
null
}
}

0 comments on commit f704238

Please sign in to comment.