Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions core/network/src/main/java/com/yapp/network/utils/ApiCallUtils.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
package com.yapp.network.utils

import com.yapp.network.model.ApiError
import kotlinx.coroutines.CancellationException
import retrofit2.HttpException
import java.io.IOException

inline fun <T> safeApiCall(action: () -> T): Result<T> =
runCatching(action).recoverCatching { exception ->
when (exception) {
is HttpException -> throw mapHttpException(exception)
is IOException -> throw ApiError("네트워크 오류 발생")
else -> throw exception
inline fun <T> safeApiCall(action: () -> T): Result<T> {
return try {
Result.success(action())
} catch (exception: Throwable) {
if (exception is CancellationException) throw exception
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


val mappedException = when (exception) {
is HttpException -> mapHttpException(exception)
is IOException -> ApiError("네트워크 오류 발생")
else -> ApiError("알 수 없는 오류 발생")
}

Result.failure(mappedException)
}
}

fun mapHttpException(exception: HttpException): ApiError {
return when (exception.code()) {
Expand Down