generated from AND-SOPT-ANDROID/and-sopt-android-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat/#8: NetworkError, Result 상태 관리 클래스 및 결과 처리 구현
- Loading branch information
1 parent
68e863c
commit 1e0250f
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
domain/src/main/java/org/sopt/and/domain/exception/NetworkError.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
30
domain/src/main/java/org/sopt/and/domain/exception/Result.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |