I am experiencing a very unusual problem where LiveDataScope is not throwing my Exception, launched in my repository. The problem is that if in place of the emit, I put a livedata event, it is dispatched to the observer, however with emit, it does not launch the event to it.
remembering that the retrofit is giving 404 on purpose, causing the catch to fall of baserepository
ViewModel:
fun launchJob(makeThis: suspend LiveDataScope<ViewState>.() -> Unit): LiveData<ViewState> =
liveData {
viewModelScope.launch {
emitLoad()
try {
makeThis(this@liveData)
} catch (error: Error){
//its work
showErrorEvent.value = Pair(error, true)
// not work
emit(ViewState.Error(error))
}
}
}
viewmodel implementation
/**
* M茅todo respons谩vel por realizar o m茅todo de reset de senha.
* */
fun requestReset() = launchJob {
emitSucess(shippingMethodRepository.resetPassword(method))
}
base repository
suspend fun getAsyncData(params: P): T {
try {
return api.invoke(params)
} catch (error: Exception) {
error.printStackTrace()
throw error
}
}
As I mentioned, changing the emit to a different livedata, it is observed by the activity, and any code before the emit is read as well, as well as Log, etc.
It's hard to tell what exactly is wrong without a complete reproducer, especially when I am not familiar with livedata.
As a guess, please make sure that you do not violate exception transparency: catching exceptions from emit and then trying to emit again is explicitly prohibited.
this is my baseRepository:
`override suspend fun getSyncData(api: suspend () -> Response ): S? {
try {
val _response = api()
if (_response.isSuccessful) {
return if (_response.code() == 200)
_response.body()
else
null
} else {
val erroMessage = try {
fromJson<ErroResponse?>(_response.errorBody()?.string() ?: "")?.message
} catch (e: Exception) {
null
} ?: _response.message()
throw when (_response.code()) {
400 -> {
BadRequestException(erroMessage)
}
500 -> {
ServerErrorException(erroMessage)
}
else -> {
RepositoryException(erroMessage)
}
}
}
} catch (error: Exception) {
error.printStackTrace()
throw error
}
}`
and this is my baseViewModel in first post,
I am not using flow, just use a bar code to update the value of my display to be able to observe, and no bar code do I issue the values.
Sorry, it is not enough information for me to make any conclusions.
If you are not using Flow at all, then it looks like an Android-specific question that is better to ask in Slack or StackOverflow