Search in sources :

Example 31 with HttpException

use of com.jakewharton.retrofit2.adapter.rxjava2.HttpException in project BaseProject by fly803.

the class ExceptionEngine method handleException.

public static ApiException handleException(Throwable e) {
    ApiException ex;
    if (e instanceof HttpException) {
        // HTTP错误
        String httpExceptionMessage = "";
        HttpException httpException = (HttpException) e;
        ex = new ApiException(e, ERROR.HTTP_ERROR);
        switch(httpException.code()) {
            case UNAUTHORIZED:
                httpExceptionMessage = UNAUTHORIZED + " " + "UNAUTHORIZED";
                break;
            case FORBIDDEN:
                httpExceptionMessage = FORBIDDEN + " " + "FORBIDDEN";
                break;
            case NOT_FOUND:
                httpExceptionMessage = NOT_FOUND + " " + "NOT_FOUND";
                break;
            case REQUEST_TIMEOUT:
                httpExceptionMessage = REQUEST_TIMEOUT + " " + "REQUEST_TIMEOUT";
                break;
            case GATEWAY_TIMEOUT:
                httpExceptionMessage = GATEWAY_TIMEOUT + " " + "GATEWAY_TIMEOUT";
                break;
            case INTERNAL_SERVER_ERROR:
                httpExceptionMessage = INTERNAL_SERVER_ERROR + " " + "INTERNAL_SERVER_ERROR";
                break;
            case BAD_GATEWAY:
                httpExceptionMessage = BAD_GATEWAY + " " + "BAD_GATEWAY";
                break;
            case SERVICE_UNAVAILABLE:
                httpExceptionMessage = SERVICE_UNAVAILABLE + " " + "SERVICE_UNAVAILABLE";
                break;
            default:
                httpExceptionMessage = "其他";
                break;
        }
        // 均视为网络错误
        ex.setMessage("网络错误:" + httpExceptionMessage);
        return ex;
    } else if (e instanceof ServerException) {
        // 服务器返回的错误
        ServerException resultException = (ServerException) e;
        ex = new ApiException(resultException, resultException.getCode());
        ex.setMessage(resultException.getMsg());
        return ex;
    } else if (e instanceof JsonParseException || e instanceof JSONException || e instanceof ParseException) {
        ex = new ApiException(e, ERROR.PARSE_ERROR);
        // 均视为解析错误
        ex.setMessage("ParseException解析错误");
        return ex;
    } else if (e instanceof ConnectException) {
        ex = new ApiException(e, ERROR.NETWORD_ERROR);
        // 均视为网络错误
        ex.setMessage("连接失败");
        return ex;
    } else if (e instanceof javax.net.ssl.SSLHandshakeException) {
        ex = new ApiException(e, ERROR.SSL_ERROR);
        ex.setMessage("证书验证失败");
        return ex;
    } else {
        ex = new ApiException(e, ERROR.UNKNOWN);
        // 未知错误
        ex.setMessage("ExceptionEngine 未知错误");
        return ex;
    }
}
Also used : JSONException(org.json.JSONException) HttpException(retrofit2.HttpException) JsonParseException(com.google.gson.JsonParseException) ParseException(java.text.ParseException) JsonParseException(com.google.gson.JsonParseException) ConnectException(java.net.ConnectException)

Aggregations

HttpException (retrofit2.HttpException)29 JSONException (org.json.JSONException)12 JsonParseException (com.google.gson.JsonParseException)11 ConnectException (java.net.ConnectException)10 ParseException (android.net.ParseException)8 SocketTimeoutException (java.net.SocketTimeoutException)7 UnknownHostException (java.net.UnknownHostException)7 Disposable (io.reactivex.disposables.Disposable)6 IOException (java.io.IOException)6 Response (retrofit2.Response)5 TextUtils (android.text.TextUtils)4 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)4 Test (org.junit.Test)4 NonNull (android.support.annotation.NonNull)3 Gson (com.google.gson.Gson)3 AccessTokenRequest (com.khmelenko.lab.varis.network.request.AccessTokenRequest)3 AuthorizationRequest (com.khmelenko.lab.varis.network.request.AuthorizationRequest)3 AccessToken (com.khmelenko.lab.varis.network.response.AccessToken)3 Authorization (com.khmelenko.lab.varis.network.response.Authorization)3 AndroidSchedulers (io.reactivex.android.schedulers.AndroidSchedulers)3