Skip to content

Commit

Permalink
[mod] #18 SignUpViewModel 예외처리 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
cacaocoffee committed Jun 3, 2024
1 parent 5b2d167 commit c3591ce
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions app/src/main/java/com/sopt/now/ui/signup/SignUpViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,11 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.sopt.now.data.api.ServicePool
import com.sopt.now.data.datasouce.request.RequestSignUpDto
import com.sopt.now.data.datasouce.response.BaseResponse
import com.sopt.now.data.model.User
import com.sopt.now.util.StringNetworkError.FAIL_ERROR
import com.sopt.now.util.StringNetworkError.SERVER_ERROR
import com.sopt.now.util.StringNetworkError.SIGNUP
import com.sopt.now.util.UiState
import kotlinx.coroutines.launch
import org.json.JSONObject
import retrofit2.Call
import retrofit2.Callback
import retrofit2.HttpException
import retrofit2.Response

class SignUpViewModel : ViewModel() {
private val authService by lazy { ServicePool.authService }
Expand All @@ -27,18 +20,21 @@ class SignUpViewModel : ViewModel() {
viewModelScope.launch {
runCatching {
authService.signUp(request)
}.onSuccess {
_signUpState.value =
UiState.Success(request.toUserWithUserId(it.headers()["userid"].toString()))
}.onSuccess { response ->
if (response.isSuccessful)
_signUpState.value =
UiState.Success(request.toUserWithUserId(response.headers()["userid"].toString()))
else {
val errorMessage =
JSONObject(response.errorBody()?.string()).getString("message")
_signUpState.value = UiState.Error(errorMessage.toString())
}
}.onFailure { e ->
if (e is HttpException) {
val errorMessage =
JSONObject(e.response()?.errorBody()?.string()).getString("message")
_signUpState.value = UiState.Error(errorMessage)
_signUpState.value = UiState.Error(e.message())
} else {
_signUpState.value = UiState.Error("코드 똑바로 짜라.. ")
_signUpState.value = UiState.Error(e.message.toString())
}

}
}
}
Expand Down

0 comments on commit c3591ce

Please sign in to comment.