When using Coroutine with Retrofit in order to call network apis, in case the server returns an error (response code != 200) we will get an Exception. My question is how to find/read the error body (in Json) sent by the server through the Exception?
try{
apiService.login()
}catch(exception:Exception){
//How to read error body from exception
//Error body example: {"ok":false,"error":"Incorrect username or password."}
}
You will just need to do this:
(exception as? HttpException)?.response()?.errorBody()?.string()
This will return the error body in Json format.