package com.github.ben.zhihudaily.data;

import com.github.ben.zhihudaily.BuildConfig;
import com.google.gson.Gson;
import com.google.gson.JsonParseException;

import retrofit2.adapter.rxjava.HttpException;

/**
 * Created by Zhou bangquan on 16/9/27.
 */


public class ResponseError {

    public int status;
    public String error_msg;

    private Throwable throwable;
    private OnResult onResult;
    private ResponseError error = null;

    public ResponseError(int status, String error_msg) {
        this.status = status;
        this.error_msg = error_msg;
    }

    public ResponseError(Throwable throwable) {
        this.throwable = throwable;
    }

    public void handle(OnResult onResult) {
        this.onResult = onResult;
        if (throwable instanceof HttpException) {
            HttpException exception = (HttpException) throwable;
            try {
                error = new Gson().fromJson(exception.response().errorBody().string(), ResponseError.class);
            } catch (Exception e) {
                if (e instanceof JsonParseException) {

                } else {

                }
            }
        } else {

        }
        if (BuildConfig.DEBUG) {
            throwable.printStackTrace();
        }
        if (null != onResult) {
            onResult.onResult(error);
        }
    }

    public interface OnResult {
        void onResult(ResponseError error);
    }
}

@@ -153,16 +153,57 @@
 ption;%0A%0A
+import java.net.SocketTimeoutException;%0A%0A
 import r
@@ -1163,32 +1163,156 @@
 %7D%0A            %7D%0A
+        %7D else if (throwable instanceof SocketTimeoutException) %7B%0A            error = new ResponseError(0, %22%E8%AF%B7%E6%B1%82%E6%97%B6%E9%97%B4%E8%BF%87%E9%95%BF,%E8%AF%B7%E7%A8%8D%E5%90%8E%E5%86%8D%E8%AF%95%22);%0A
         %7D else %7B

add SocketTimeoutException