Reputation: 164
I'm using Retrofit2+RxJava. When i make request server can return one of two response Succes and Error but in both cases HTTP STATUS CODE = 200. How do achieve that when i get Erron in response body instead calls onNext calls onError?
Observable<CreateAuth> createAuthObservable = endApiServiceProvider.createAuth(phone);
Subscription subscription = createAuthObservable
.observeOn(AndroidSchedulers.mainThread())
.subscribe(createAuth -> {
hideProgress();
successAuth(phone);
},
throwable -> {
//todo correct handle error
hideProgress();
if (throwable instanceof IOException) {
showError("Network error. Please check your internet connection");
}
});
My endpoint interface
@GET("/api/new.php")
Observable<CreateAuth> createAuth(@Query("username") String uname);
Succes Response (HTTP code 200) and CreateAuth.java POJO structure as below
{
"val": true,
"message": "Success!"
}
Error Response (HTTP code 200) and ErrorResponse.java POJO structure as below
{
"error_type": "wrong_c",
"error_additional": "Illegal char in name!"
}
Upvotes: 1
Views: 397
Reputation: 10280
I'd say this is a server-side issue rather than a problem with your client-side code. You receive HTTP 200 for both success and error responses, then this would not automatically throw an error in Retrofit. As far as Retrofit is concerned, the request was a success.
You'd need to analyse what was returned and handle it appropriately if you want to keep your server configuration the same, therefore both success and error responses must me parsable into the same class.
Alternatively, re-configure your endpoints to return a 4xx response on error. I'd recommend this approach.
Upvotes: 4