"is not a valid element" Gives a fault
If the server is down when I log in to the application, an error message should normally be written and the response value returns 500.
AppApi.kt
@POST(Constants.LOGIN_ENDPOINT)
@Headers("No-Authentication: true")
fun requestLogin(@Body loginModel: LoginModel): Observable<LoginResponse>
LoginRepository.kt
fun requestLogin(loginModel: LoginModel): Observable<LoginResponse> {
return appApi.requestLogin(loginModel)
}
LoginViewModel.kt
fun requestLogin(loginModel: LoginModel): MutableLiveData<ResponseControl<LoginResponse?>> {
loginRepository.requestLogin(loginModel).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()).subscribe({
requestLoginMutableLiveData.postValue(ResponseControl.Success(it))
}, {
sendError(
ErrorModel(
exceptionMessage = it.localizedMessage,
innerExceptionStackTrace = it.stackTraceToString()
)
)
//but it doesn't come here
if (it is HttpException) {
if (it.code() == 401) {
requestLoginMutableLiveData.postValue(ResponseControl.AuthorizationError())
} else {
val errorMessage = it.response()?.errorBody()?.string()
val errorDetail = Gson().fromJson(errorMessage, ApiError::class.java)
requestLoginMutableLiveData.postValue(
ResponseControl.Error(
message = errorDetail.message,
data = null
)
)
}
} else {
requestLoginMutableLiveData.postValue(
ResponseControl.Error(
null,null
)
)
}
}).let {
compositeDisposable.add(it)
}
return requestLoginMutableLiveData
}
//LoginActivity
viewModel.requestLoginMutableLiveData.observe(this) { response ->
when (response) {
is ResponseControl.Success -> {
//Login is Success
saveTokens(response.data!!)
}
is ResponseControl.Error -> {
if (response.message.isNullOrEmpty()) {
showError(getString(R.string.an_error_occured))
} else {
showError(getStringWithKey(response.message))
}
binding.loginButton.text = getString(R.string.login)
binding.buttonProgressBar.gone()
binding.loginButton.isClickable = true
}
else -> {}
}
}
Is there any wrong part in my code? Can you please correct it?