Skip to content

Commit

Permalink
[TNT-164] feat: NetworkHandler 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
hoyahozz committed Jan 25, 2025
1 parent 1052df9 commit dc072c1
Showing 1 changed file with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package co.kr.data.network.util

import co.kr.data.network.model.base.BaseErrorResponse
import co.kr.data.network.model.exception.NetworkException
import kotlinx.serialization.json.Json
import retrofit2.HttpException
import retrofit2.Response

internal suspend inline fun <T> networkHandler(
crossinline call: suspend () -> T,
): T = try {
call()
} catch (httpException: HttpException) {
val errorResponse = parseErrorResponse(httpException.response())
val message = errorResponse.message

throw when (httpException.code()) {
400 -> NetworkException.BadRequestException(message)
401 -> NetworkException.UnauthorizedException(message)
403 -> NetworkException.ForbiddenException(message)
404 -> NetworkException.NotFoundException(message)
409 -> NetworkException.ConflictException(message)
in 500 until 600 -> NetworkException.ServerException(message)
else -> NetworkException.UnknownException(message)
}
}

private fun parseErrorResponse(response: Response<*>?): BaseErrorResponse {
return try {
requireNotNull(
response?.errorBody()?.string()?.let {
Json.decodeFromString<BaseErrorResponse>(it)
},
)
} catch (e: Exception) {
throw NetworkException.UnknownException("Failed to parse error message,\n${e.message}")
}
}

0 comments on commit dc072c1

Please sign in to comment.