Java Code Examples for retrofit2.adapter.rxjava.HttpException#code()

The following examples show how to use retrofit2.adapter.rxjava.HttpException#code() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExceptionHandle.java    From xifan with Apache License 2.0 4 votes vote down vote up
public static ApiException handleException(Throwable e) {
    e.printStackTrace();
    Logger.e("http request error result:\n" + e.fillInStackTrace());
    Context context = App.getInstance().getApplicationContext();

    if (e instanceof HttpException) {
        ApiException apiException = null;
        HttpException httpException = (HttpException) e;
        String errorMessage = null;
        switch (httpException.code()) {
            case UNAUTHORIZED:
                errorMessage = context.getString(R.string.http_unauthorized_error);
                break;
            case FORBIDDEN:
            case NOT_FOUND:
            case REQUEST_TIMEOUT:
            case RANGE_NOT_SATISFIABLE:
            case INTERNAL_SERVER_ERROR:
            case BAD_GATEWAY:
            case SERVICE_UNAVAILABLE:
            case GATEWAY_TIMEOUT:
                errorMessage = context.getString(R.string.http_service_error);
                break;
        }

        try {
            String errorBody = httpException.response().errorBody().string();
            if (!TextUtils.isEmpty(errorBody)) {
                errorMessage = errorBody;
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        apiException = new ApiException(ErrorCode.ERROR_UNAUTHORIZED, errorMessage);
        return apiException;
    }

    if (e instanceof ConnectException) {
        return new ApiException(ErrorCode.ERROR_NO_CONNECT,
                context.getString(R.string.http_connect_error));
    }

    if (e instanceof JsonParseException || e instanceof JsonSyntaxException) {
        return new ApiException(ErrorCode.ERROR_PARSE,
                context.getString(R.string.http_data_parse_error));
    }

    if (e instanceof SocketTimeoutException) {
        return new ApiException(ErrorCode.ERROR_NET_TIMEOUT,
                context.getString(R.string.http_connect_timeout));
    }
    return new ApiException(ErrorCode.ERROR_UNKNOWN,
            context.getString(R.string.http_unknow_error));
}
 
Example 2
Source File: ExceptionHandle.java    From RetrofitClient with Apache License 2.0 4 votes vote down vote up
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:
            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.code);
        ex.message = resultException.message;
        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 ConnectTimeoutException){
        ex = new ResponeThrowable(e, ERROR.TIMEOUT_ERROR);
        ex.message = "连接超时";
        return ex;
    } else if (e instanceof java.net.SocketTimeoutException) {
        ex = new ResponeThrowable(e, ERROR.TIMEOUT_ERROR);
        ex.message = "连接超时";
        return ex;
    }
    else {
        ex = new ResponeThrowable(e, ERROR.UNKNOWN);
        ex.message = "未知错误";
        return ex;
    }
}
 
Example 3
Source File: ErrorWrapper.java    From android with Apache License 2.0 4 votes vote down vote up
static Throwable wrapError(Retrofit retrofit, Throwable e) {
    if (e instanceof HttpException) {
        HttpException he = (HttpException) e;

        ErrorResponse r = parseError(retrofit, he.response());

        if (r != null && r.message != null) {
            if (r.message.contains("No provider support found for coordinate")) {
                UnsupportedCoordinatesException uce = new UnsupportedCoordinatesException(r.message, e);
                uce.setProviderName(r.provider);

                return uce;
            }

            if (r.message.contains("Unknown place code")) {
                UnknownPlaceCodeException upce = new UnknownPlaceCodeException(r.message, e);
                upce.setProviderName(r.provider);

                return upce;
            }

            if (r.message.contains("Data format at e-solat") || r.message.contains("Error connecting to")) {
                PrayerProviderException ppe = new PrayerProviderException(r.message, e);

                if (!r.hasProviderName()) {
                    ppe.setProviderName("JAKIM");
                } else {
                    ppe.setProviderName(r.provider);
                }

                return ppe;
            }
        }

        return new ServerException("Server error " + he.code(), he);
    }

    if (e instanceof MalformedJsonException) {
        return new ServerException("Malformed JSON", e);
    }

    return e;
}
 
Example 4
Source File: AbsSubscriber.java    From fingerpoetry-android with Apache License 2.0 4 votes vote down vote up
@Override
public void onError(Throwable e) {
    e.printStackTrace();
    dismissProgressDialog();
    Throwable throwable = e;
    //获取最根源的异常
    while(throwable.getCause() != null){
        e = throwable;
        throwable = throwable.getCause();
    }
    ApiException ex;
    if (e instanceof HttpException){             //HTTP错误
        HttpException httpException = (HttpException) e;
        ex = new ApiException(e, httpException.code());
        try {
            String str = httpException.response().errorBody().string();
            JSONObject jsonObject = new JSONObject(str);
            int status = jsonObject.getInt("status");
            ex.setCode(status);
            if(jsonObject.has("msg")){
                ex.setMessage(jsonObject.getString("msg"));
            }
        } catch (Exception e1) {
            ex.setCode(SERVICE_UNAVAILABLE);
        }
        switch(httpException.code()){
            case UNAUTHORIZED:
                onPermissionError(ex);          //权限错误,需要实现
                break;
            case FORBIDDEN:
            case INVALIDPARAM:
            case NOT_FOUND:
            case REQUEST_TIMEOUT:
                onError(ex);
                break;
            case GATEWAY_TIMEOUT:
            case INTERNAL_SERVER_ERROR:
            case BAD_GATEWAY:
            case SERVICE_UNAVAILABLE:
            default:
                ex.setMessage(BookBoxApplication.getInstance().getString(R.string.net_error));  //均视为网络错误
                onError(ex);
                break;
        }
    } else if (e instanceof SocketTimeoutException
            || e instanceof ConnectException) {
        ex = new ApiException(e, ApiException.UNKNOWN);
        ex.setMessage(BookBoxApplication.getInstance().getString(R.string.net_disconnect));  //均视为网络错误
        onError(ex);
    }else if (e instanceof JsonParseException
            || e instanceof JSONException){
        ex = new ApiException(e, ApiException.PARSE_ERROR);
        ex.setMessage(BookBoxApplication.getInstance().getString(R.string.parse_error));            //均视为解析错误
        onError(ex);
    } else {
        ex = new ApiException(e, ApiException.UNKNOWN);
        ex.setMessage(unknownMsg);          //未知错误
        onError(ex);
    }
}
 
Example 5
Source File: ExceptionEngine.java    From Retrofit2RxjavaDemo with Apache License 2.0 4 votes vote down vote up
public static ApiException handleException(Throwable e){
    ApiException ex;
    if (e instanceof HttpException){             //HTTP错误
        HttpException httpException = (HttpException) e;
        ex = new ApiException(e, ErrorType.HTTP_ERROR);
        switch(httpException.code()){
            case UNAUTHORIZED:
                ex.message = "当前请求需要用户验证";
                break;
            case FORBIDDEN:
                ex.message = "服务器已经理解请求,但是拒绝执行它";
                break;
            case NOT_FOUND:
                ex.message = "服务器异常,请稍后再试";
                break;
            case REQUEST_TIMEOUT:
                ex.message = "请求超时";
                break;
            case GATEWAY_TIMEOUT:
                ex.message = "作为网关或者代理工作的服务器尝试执行请求时,未能及时从上游服务器(URI标识出的服务器,例如HTTP、FTP、LDAP)或者辅助服务器(例如DNS)收到响应";
                break;
            case INTERNAL_SERVER_ERROR:
                ex.message = "服务器遇到了一个未曾预料的状况,导致了它无法完成对请求的处理";
                break;
            case BAD_GATEWAY:
                ex.message = "作为网关或者代理工作的服务器尝试执行请求时,从上游服务器接收到无效的响应";
                break;
            case SERVICE_UNAVAILABLE:
                ex.message = "由于临时的服务器维护或者过载,服务器当前无法处理请求";
                break;
            default:
                ex.message = "网络错误";  //其它均视为网络错误
                break;
        }
        return ex;
    } else if (e instanceof ServerException){    //服务器返回的错误
        ServerException resultException = (ServerException) e;
        ex = new ApiException(resultException, resultException.code);
        ex.message = resultException.message;
        return ex;
    } else if (e instanceof JsonParseException
            || e instanceof JSONException
            || e instanceof ParseException){
        ex = new ApiException(e, ErrorType.PARSE_ERROR);
        ex.message = "解析错误";            //均视为解析错误
        return ex;
    }else if(e instanceof ConnectException || e instanceof SocketTimeoutException || e instanceof ConnectTimeoutException){
        ex = new ApiException(e, ErrorType.NETWORD_ERROR);
        ex.message = "连接失败";  //均视为网络错误
        return ex;
    }
    else {
        ex = new ApiException(e, ErrorType.UNKNOWN);
        ex.message = "未知错误";          //未知错误
        return ex;
    }
}