Search in sources :

Example 51 with HttpException

use of retrofit2.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 HttpException (retrofit2.adapter.rxjava.HttpException)18 Test (org.junit.Test)14 JSONException (org.json.JSONException)12 JsonParseException (com.google.gson.JsonParseException)11 Intent (android.content.Intent)10 ConnectException (java.net.ConnectException)10 Bundle (android.os.Bundle)9 ParseException (android.net.ParseException)8 SocketTimeoutException (java.net.SocketTimeoutException)7 UnknownHostException (java.net.UnknownHostException)7 Response (retrofit2.Response)7 View (android.view.View)6 Build (com.github.vase4kin.teamcityapp.buildlist.api.Build)6 Disposable (io.reactivex.disposables.Disposable)6 IOException (java.io.IOException)6 TextView (android.widget.TextView)5 List (java.util.List)5 TextUtils (android.text.TextUtils)4 BuildCancelRequest (com.github.vase4kin.teamcityapp.build_details.api.BuildCancelRequest)4