mending3
06/17/2020, 3:05 PMviewModelScope.launch {
try {
val apiRes = AccountRepository().connect.createAccount(accountName.value, phoneNumber.value?.toInt())
_showNotif.postValue("success")
} catch (throwable: Throwable) {
when (throwable) {
is IOException -> _showNotif.postValue(throwable.message)
is HttpException -> _showNotif.postValue("${throwable.code()}")
else -> _showNotif.postValue(throwable.message)
}
}
}
I want to catch an exception from retrofit. the api returns 500. however why does the viewModelScope return me the success message indicated by _showNotif.postValue("success")
code?Rémy
06/17/2020, 3:08 PMmending3
06/17/2020, 3:10 PMmending3
06/17/2020, 3:10 PMJoost Klitsie
06/17/2020, 3:15 PMJoost Klitsie
06/17/2020, 3:15 PMRémy
06/17/2020, 3:16 PMJoost Klitsie
06/17/2020, 3:16 PMinline fun <reified T> Call<T>.executeForResult(): Result<T> =
try {
val response = execute()
val body = response.body()
when {
response.isSuccessful && body != null -> SUCCESS(body)
response.isSuccessful && response.code() == 204 && T::class == Any::class -> SUCCESS(Any() as T)
response.isSuccessful -> ERROR(error = NullPointerException())
else ->ERROR(error = HttpException(response))
}
}
catch (e: Exception) {
RsmLog.e(BUSINESS_TAG, e)
ERROR(error = e)
}
I have for example this, where it returns a Result object, which can be SUCCESS or ERRORJoost Klitsie
06/17/2020, 3:16 PMJoost Klitsie
06/17/2020, 3:16 PMJoost Klitsie
06/17/2020, 3:16 PMJoost Klitsie
06/17/2020, 3:17 PMmending3
06/17/2020, 3:18 PMtry {
val apiRes = AccountRepository().connect.createAccount(accountName.value, phoneNumber.value?.toInt())
if (apiRes.code() == 500) {
_showNotif.postValue("error 500")
}
}
it should be caught in the catch blockmending3
06/17/2020, 3:18 PMmending3
06/17/2020, 3:23 PMRémy
06/17/2020, 3:24 PM