2

I have a REST Server, My problem is whenever my response is not successful i want to parse the error from response body and show it to the user (pass the error info to the calling Activity)

@Named("rest_api")
@Singleton
@Provides
Interceptor provideRESTInterceptor(final UserManager userManager) {
    return new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request original = chain.request();
            String token = userManager.getJwt();

            Request request = original.newBuilder()
                    .header("Accept", "application/json")
                    .header("Authorization", "Bearer "+token)
                    .method(original.method(), original.body())
                    .build();

            Log.i(LOG_TAG, "REQUEST URL "+request.uri());
            Response response = chain.proceed(request);

            if(!response.isSuccessful()) {
                // How do i send this error to my Activity.

                APIError apiError = new APIError(response.code(),response.body().string());
            }

            // Customize or return the response
            return response;
        }
    };
}

I am using RxJavaConverter

@Named("rest_api")
@Singleton
@Provides
Retrofit provideRESTRetrofit(@Named("rest_api")OkHttpClient client, Converter.Factory converter, CallAdapter.Factory rxJava) {
    return new Retrofit.Builder()
            .baseUrl(PUBLIC_PRODUCTION_URL)
            .client(client)
            .addCallAdapterFactory(rxJava)
            .addConverterFactory(converter)
            .build();
}

Since Retrofit 2.0, even if the response is not successful it tries to convert the data with given GSON into (POJO), and thus throw an error, and i lost the actual message of the error.

Bikash
  • 1,452
  • 1
  • 15
  • 24
  • Have a look at this: http://stackoverflow.com/questions/33774940/get-response-status-code-using-retrofit-2-0-and-rxjava/33775529#33775529. I think you could remove the success check from the `Interceptor` and instead check later in the `Subscriber` - or maybe add a `Transformer` to your Rx chain that, for every request, checks for success and wraps failures in an `APIError`. Would that solve your problem? – david.mihola Mar 22 '16 at 07:07
  • @david.mihola Actually i am using ResponseBody now, but than that kind of compromise with my need to auto convert the response to POJO or using Gson converter to Retrofit, never the less for now i can continue with that :) – Bikash Mar 22 '16 at 15:49
  • Oh, I am sorry, that's not how I meant it. I only meant the "outer" part, i.e. you can use `Observable>` and will have access to both the `Response` (with HTTP reponse codes, etc.) AND the parsed `YourModelClass` (which is in `response.body()`). – david.mihola Mar 22 '16 at 17:05
  • Ohh Really can we do that, Let me try it. – Bikash Mar 22 '16 at 17:06
  • @david.mihola Today i got time to check it and guess what, it works like a charm. – Bikash Mar 24 '16 at 08:53

1 Answers1

2

As @david:mihola suggested it worked, using Retrofit Response object i was able to achieve this.

Response<MyObject> response;
if(response.isSucessful) {
   MyObject obj = response.body();
} else {
  // You can process your error message from the reponse
}

It really saved me from lot manual GSON parsing

Bikash
  • 1,452
  • 1
  • 15
  • 24