Skip to content

Instantly share code, notes, and snippets.

@rezaiyan
Last active October 28, 2018 07:00
Show Gist options
  • Save rezaiyan/70950cce356e50ba88e53d7b2c9eecfd to your computer and use it in GitHub Desktop.
Save rezaiyan/70950cce356e50ba88e53d7b2c9eecfd to your computer and use it in GitHub Desktop.
package com.ott.common.di;
/**
* @author rezaiyan (alirezaiyann@gmail.com)
* @since 6:29 PM.
*/
import android.support.annotation.NonNull;
import com.google.gson.Gson;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.net.NoRouteToHostException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import io.reactivex.Observable;
import com.ott.BuildConfig;
import com.ott.common.common.Const;
import com.ott.common.model.ErrorModel;
import retrofit2.Call;
import retrofit2.CallAdapter;
import retrofit2.HttpException;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
/**
* A sample showing a custom {@link CallAdapter} which adapts the built-in {@link Call} to a custom
* version whose callback has more granular methods.
*/
public class ErrorHandlingAdapter extends CallAdapter.Factory {
private final RxJava2CallAdapterFactory original;
private ErrorHandlingAdapter() {
original = RxJava2CallAdapterFactory.create();
}
public static CallAdapter.Factory create() {
return new ErrorHandlingAdapter();
}
@SuppressWarnings("unchecked")
@Override
public CallAdapter<?, ?> get(@NonNull Type returnType, @NonNull Annotation[] annotations, @NonNull Retrofit retrofit) {
return new RxCallAdapterWrapper(original.get(returnType, annotations, retrofit));
}
@SuppressWarnings("ConstantConditions")
private static class RxCallAdapterWrapper<R> implements CallAdapter<R, Object> {
private final CallAdapter<R, Object> wrapped;
RxCallAdapterWrapper(CallAdapter<R, Object> wrapped) {
this.wrapped = wrapped;
}
@Override
public Type responseType() {
return wrapped.responseType();
}
/**
* To Handle server error
* we have some predefine code such as 422 412 416 500
* type of error body is {status:Boolean, msg:String, success:Boolean}
* All of error codes pass by cause of a throwable which to handle in presenter classes
*/
@SuppressWarnings("unchecked")
@Override
public Object adapt(@NonNull Call<R> call) {
Object result = wrapped.adapt(call);
if (result instanceof Observable) {
return ((Observable) result).onErrorResumeNext(throwable -> {
if ((throwable instanceof UnknownHostException)
|| throwable instanceof NoRouteToHostException
|| (throwable instanceof SocketTimeoutException)
|| (throwable instanceof SocketException)
|| ((throwable instanceof HttpException) && ((HttpException) throwable).code() == 504)) {
String message = (BuildConfig.FLAVOR_parent != Const.FLAVOR_ENGLISH) ? "اینترنت دردسترس نیست" : "No internet connection";
return Observable.error(new Throwable(message, new Throwable("0")));
} else if ((throwable instanceof HttpException) && ((HttpException) throwable).code() == 500) {
String message = (BuildConfig.FLAVOR_parent != Const.FLAVOR_ENGLISH) ? "خطایی سمت سرور اتقاق افتاده است" : "Server error";
return Observable.error(new Throwable(message, new Throwable("0")));
}
if ((throwable instanceof HttpException) && (((HttpException) throwable).code() == 404)) {
return Observable.error(new Throwable("Not Found", new Throwable("0")));
} else {
try {
Response response = ((HttpException) throwable).response();
ErrorModel jsonObject = new Gson().fromJson(response.errorBody().string(), ErrorModel.class);
throwable = new Throwable(jsonObject.getMsg(), new Throwable(String.valueOf(((HttpException) throwable).code())));
return Observable.error((Throwable) throwable);
} catch (IOException e) {
return Observable.error(new Throwable("", new Throwable(String.valueOf(((HttpException) throwable).code()))));
}
}
});
}
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment