Search in sources :

Example 1 with ApiException

use of com.cg.baseproject.request.exception.ApiException in project BaseProject by fly803.

the class DeprecatedExceptionHandle method handleException.

public static ResponeThrowable handleException(Throwable e) {
    ResponeThrowable ex;
    if (e instanceof HttpException) {
        HttpException httpException = (HttpException) e;
        ex = new ResponeThrowable(e, ERROR.HTTP_ERROR);
        switch(httpException.code()) {
            case UNAUTHORIZED:
            case FORBIDDEN:
                ex.message = "Forbidden";
                break;
            case NOT_FOUND:
            case REQUEST_TIMEOUT:
            case GATEWAY_TIMEOUT:
            case INTERNAL_SERVER_ERROR:
            case BAD_GATEWAY:
            case SERVICE_UNAVAILABLE:
            default:
                ex.message = "网络错误";
                break;
        }
        return ex;
    } else if (e instanceof ServerException) {
        ServerException resultException = (ServerException) e;
        ex = new ResponeThrowable(resultException, resultException.getCode());
        ex.message = resultException.getMessage();
        return ex;
    } else if (e instanceof JsonParseException || e instanceof JSONException || e instanceof ParseException) {
        ex = new ResponeThrowable(e, ERROR.PARSE_ERROR);
        ex.message = "解析错误";
        return ex;
    } else if (e instanceof ConnectException) {
        ex = new ResponeThrowable(e, ERROR.NETWORD_ERROR);
        ex.message = "连接失败";
        return ex;
    } else if (e instanceof javax.net.ssl.SSLHandshakeException) {
        ex = new ResponeThrowable(e, ERROR.SSL_ERROR);
        ex.message = "证书验证失败";
        return ex;
    } else if (e instanceof ApiException) {
        ex = new ResponeThrowable(e, ((ApiException) e).getCode());
        ex.message = e.getMessage();
        return ex;
    } else {
        ex = new ResponeThrowable(e, ERROR.UNKNOWN);
        ex.message = "未知错误ExceptionHandle";
        return ex;
    }
}
Also used : ServerException(com.cg.baseproject.request.exception.ServerException) JSONException(org.json.JSONException) HttpException(retrofit2.HttpException) JsonParseException(com.google.gson.JsonParseException) ParseException(android.net.ParseException) JsonParseException(com.google.gson.JsonParseException) ConnectException(java.net.ConnectException) ApiException(com.cg.baseproject.request.exception.ApiException)

Aggregations

ParseException (android.net.ParseException)1 ApiException (com.cg.baseproject.request.exception.ApiException)1 ServerException (com.cg.baseproject.request.exception.ServerException)1 JsonParseException (com.google.gson.JsonParseException)1 ConnectException (java.net.ConnectException)1 JSONException (org.json.JSONException)1 HttpException (retrofit2.HttpException)1