Skip to content

Commit

Permalink
feat/#8: NetworkError, Result 상태 관리 클래스 및 결과 처리 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
kangyein9892 committed Nov 15, 2024
1 parent 68e863c commit 1e0250f
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
41 changes: 41 additions & 0 deletions domain/src/main/java/org/sopt/and/domain/exception/NetworkError.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.sopt.and.domain.exception

sealed class NetworkError : Throwable(){
abstract val title: String
abstract override val message: String
}

data object ConnectError : NetworkError() {
override val title: String
get() = "서버에 연결할 수 없어요. "
override val message: String
get() = "인터넷 연결을 확인한 후 다시 시도해주세요."
}

data object TimeoutError : NetworkError() {
override val title: String
get() = "서버 응답 시간이 초과되었어요 ."
override val message: String
get() = "잠시 후 다시 시도해주세요."
}

data object UnknownHostError : NetworkError() {
override val title: String
get() = "서버를 찾을 수 없습니다. "
override val message: String
get() = "인터넷 연결 상태를 확인해주세요."
}

data object UnknownError : NetworkError() {
override val title: String
get() = "알 수 없는 오류가 발생했어요. "
override val message: String
get() = "잠시 후 다시 시도해주세요. "
}

data class HttpCodeError(val statusCode: String, val code: String) : NetworkError() {
override val title: String
get() = statusCode
override val message: String
get() = code
}
30 changes: 30 additions & 0 deletions domain/src/main/java/org/sopt/and/domain/exception/Result.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.sopt.and.domain.exception

sealed class Result<out T> {
data class Success<T>(val value: T) : Result<T>()
data class Error<T>(val error: NetworkError) : Result<T>()
}

fun <T> Result<T>.onSuccess(action: (T) -> Unit): Result<T> {
return when (this) {
is Result.Success<T> ->{
action(value)
this
}
is Result.Error<T> -> {
this
}
}
}

fun <T> Result<T>.onError(action: (NetworkError) -> Unit): Result<T> {
return when (this) {
is Result.Success<T> ->{
this
}
is Result.Error<T> -> {
action(error)
this
}
}
}

0 comments on commit 1e0250f

Please sign in to comment.