Search in sources :

Example 26 with HttpException

use of com.jakewharton.retrofit2.adapter.rxjava2.HttpException in project 91Pop by DanteAndroid.

the class ApiException method handleException.

public static ApiException handleException(Throwable e) {
    // 使用RxCache之后返回的是包裹的CompositeException,一般包含2个异常,rxcache异常和原本的异常
    Logger.t(TAG).d("开始解析错误------");
    if (e instanceof CompositeException) {
        CompositeException compositeException = (CompositeException) e;
        for (Throwable throwable : compositeException.getExceptions()) {
            if (!(throwable instanceof RxCacheException)) {
                e = throwable;
                Logger.t(TAG).d("其他异常:" + throwable.getMessage());
            } else {
                Logger.t(TAG).d("RxCache 异常");
            }
        }
    }
    ApiException ex;
    if (e instanceof HttpException) {
        HttpException httpException = (HttpException) e;
        ex = new ApiException(httpException, httpException.code());
        ex.message = httpException.getMessage();
        return ex;
    } else if (e instanceof JsonParseException || e instanceof JSONException || e instanceof JsonSerializer || e instanceof NotSerializableException || e instanceof ParseException) {
        ex = new ApiException(e, Error.PARSE_ERROR);
        ex.message = "数据解析错误";
        return ex;
    } else if (e instanceof ClassCastException) {
        ex = new ApiException(e, Error.CAST_ERROR);
        ex.message = "类型转换错误";
        return ex;
    } else if (e instanceof ConnectException) {
        ex = new ApiException(e, Error.NETWORD_ERROR);
        ex.message = "连接失败";
        return ex;
    } else if (e instanceof javax.net.ssl.SSLHandshakeException) {
        ex = new ApiException(e, Error.SSL_ERROR);
        ex.message = "证书验证失败";
        return ex;
    } else if (e instanceof ConnectTimeoutException) {
        ex = new ApiException(e, Error.TIMEOUT_ERROR);
        ex.message = "网络连接超时";
        return ex;
    } else if (e instanceof java.net.SocketTimeoutException) {
        ex = new ApiException(e, Error.TIMEOUT_ERROR);
        ex.message = "网络连接超时";
        return ex;
    } else if (e instanceof UnknownHostException) {
        ex = new ApiException(e, Error.UNKNOWNHOST_ERROR);
        ex.message = "无法解析该域名";
        return ex;
    } else if (e instanceof NullPointerException) {
        ex = new ApiException(e, Error.NULLPOINTER_EXCEPTION);
        ex.message = "NullPointerException";
        return ex;
    } else if (e instanceof VideoException) {
        ex = new ApiException(e, Error.PARSE_VIDEO_URL_ERROR);
        ex.message = e.getMessage();
        return ex;
    } else if (e instanceof FavoriteException) {
        ex = new ApiException(e, Error.FAVORITE_VIDEO_ERROR);
        ex.message = e.getMessage();
        return ex;
    } else if (e instanceof DaoException) {
        ex = new ApiException(e, Error.GREEN_DAO_ERROR);
        ex.message = "数据库错误";
        return ex;
    } else if (e instanceof MessageException) {
        ex = new ApiException(e, Error.COMMON_MESSAGE_ERROR);
        ex.message = e.getMessage();
        return ex;
    } else {
        ex = new ApiException(e, Error.UNKNOWN);
        ex.message = "未知错误:" + e.getMessage();
        return ex;
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) CompositeException(io.reactivex.exceptions.CompositeException) RxCacheException(io.rx_cache2.RxCacheException) JSONException(org.json.JSONException) JsonSerializer(com.google.gson.JsonSerializer) JsonParseException(com.google.gson.JsonParseException) DaoException(org.greenrobot.greendao.DaoException) NotSerializableException(java.io.NotSerializableException) HttpException(retrofit2.HttpException) JsonParseException(com.google.gson.JsonParseException) ParseException(android.net.ParseException) ConnectException(java.net.ConnectException) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException)

Example 27 with HttpException

use of com.jakewharton.retrofit2.adapter.rxjava2.HttpException in project dagger-test-example by aschattney.

the class SimpleEspressoTest method errorDialogIsShownWhenRequestFails.

@Test
public void errorDialogIsShownWhenRequestFails() {
    final String message = "some exception message";
    when(weatherApi.getCurrentWeather(FAKE_LONGITUDE, FAKE_LATITUDE)).thenReturn(Observable.error(new HttpException(Response.error(500, ResponseBody.create(MediaType.parse("text/plain"), message)))));
    when(weatherApi.getTomorrowWeather(FAKE_LONGITUDE, FAKE_LATITUDE)).thenReturn(Observable.empty());
    rule.launchActivity(null);
    this.allowPermissionsIfNeeded();
    onView(withText(message)).check(matches(isDisplayed()));
}
Also used : HttpException(retrofit2.HttpException) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 28 with HttpException

use of com.jakewharton.retrofit2.adapter.rxjava2.HttpException in project BBS-Android by bdpqchen.

the class SimpleObserver method onError.

@Override
public void onError(Throwable throwable) {
    LogUtil.dd("onError() in SimpleObserver");
    String msg = throwable.getMessage();
    if (TextUtils.isEmpty(msg)) {
        msg = "网络错误";
    }
    if (throwable instanceof SocketTimeoutException) {
        msg = "网络请求超时...请重试";
    } else if (throwable instanceof UnknownHostException) {
        msg = "找不到服务器了..";
    } else if (throwable instanceof ResponseException) {
        msg = throwable.getMessage();
        LogUtil.dd("response exception is cased");
        LogUtil.dd("the msg is", throwable.getMessage());
    } else if (throwable instanceof HttpException) {
        HttpException exception = (HttpException) throwable;
        try {
            String errorBody = exception.response().errorBody().string();
            JSONObject errorJsonObject = new JSONObject(errorBody);
            // int errCode = errorJsonObject.getInt("err");
            msg = errorJsonObject.getString("data");
        } catch (IOException | JSONException e) {
            e.printStackTrace();
        }
    }
    _onError(msg);
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) UnknownHostException(java.net.UnknownHostException) JSONObject(org.json.JSONObject) HttpException(retrofit2.HttpException)

Example 29 with HttpException

use of com.jakewharton.retrofit2.adapter.rxjava2.HttpException in project android-client by GenesisVision.

the class RetrofitException method asRetrofitException.

public static RetrofitException asRetrofitException(Throwable throwable) {
    // We had non-200 http error
    if (throwable instanceof HttpException) {
        HttpException httpException = (HttpException) throwable;
        Response response = httpException.response();
        return RetrofitException.httpError(response.raw().request().url().toString(), response);
    }
    // A network error happened
    if (throwable instanceof IOException) {
        return RetrofitException.networkError((IOException) throwable);
    }
    // We don't know what happened. We need to simply convert to an unknown error
    return RetrofitException.unexpectedError(throwable);
}
Also used : Response(retrofit2.Response) HttpException(retrofit2.HttpException) IOException(java.io.IOException)

Example 30 with HttpException

use of com.jakewharton.retrofit2.adapter.rxjava2.HttpException 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

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