package com.zhaohaoting.framework.requset.rxjava_retrofit;

import com.google.gson.JsonSyntaxException;
import com.zhaohaoting.framework.utils.Logger;

import java.net.ConnectException;
import java.net.SocketTimeoutException;

import io.reactivex.FlowableSubscriber;

/**
 * Created by Administrator on 2018/5/21.
 */

public abstract class RxResultSimpleSubscriber<DATA> implements FlowableSubscriber<DATA> {

    protected static final int FAILURE = 1;//失败
    protected static final int LOCAL_FAILURE = -1000;//本地错误
    public static final int REQUEST_SUCCESS = 0;//请求成功
    public static final int NEED_LOGIN = 6666;//需要从新登录


    @Override
    public void onError(Throwable e) {
        e.printStackTrace();
        Logger.e("RxResultSubscriber", e.getMessage());
        String tipMsg;//错误提示
        if (e instanceof JsonSyntaxException) {
            tipMsg = "数据解析出错";
        } else if (e instanceof SocketTimeoutException) {
            tipMsg = "链接超时";
        } else if (e instanceof ConnectException) {
            if (e.getMessage().contains("Network is unreachable"))
                tipMsg = "无网络";
            else if (e.getMessage().contains("failed to connect to")) {
                tipMsg = "链接服务器失败";
            } else {
                tipMsg = "网络不给力啊";
            }
        } else {
            tipMsg = "异常";
        }
        onFailure(FAILURE, tipMsg);
    }

    protected void onFailure(int code, String tipMsg) {
    }

    @Override
    public void onComplete() {

    }
}
