My Android kotlin code:
viewModelScope.launch(Dispatchers.IO) {
_userAuth.postValue((Resource.Loading()))
try {
val res = repository.userSignUp(SignupRequest("test", "test@test.com", "password"))
if (res.isSuccessful) {
res.body()?.let {
_userAuth.postValue(Resource.Success(it))
}
} else {
// Handle error here...
}
} catch (e: IOException) {
Timber.d(e.message)
} catch (e: HttpException) {
Timber.d(e.message)
}
}
}
I developed nodejs api where user can Signup and Signin. How to handle errors if reponse is not successful. My json response error model look like this:
{
"error": {
"message": "Username is already taken",
"code": 422
}
}
error.message property's value could be anything according to error e.g. Email is already taken, Email is invalid, etc. How to parse with own custom kotlin data class?