Search in sources :

Example 16 with HttpException

use of com.jakewharton.retrofit2.adapter.rxjava2.HttpException in project Varis-Android by dkhmelenko.

the class TestAuthPresenter method testTwoFactorAuth.

@Test
public void testTwoFactorAuth() {
    final String login = "login";
    final String password = "password";
    String auth = EncryptionUtils.generateBasicAuthorization(login, password);
    // rules for throwing a request for 2-factor auth
    final String expectedUrl = "https://sample.org";
    Request rawRequest = new Request.Builder().url(expectedUrl).build();
    okhttp3.Response rawResponse = new okhttp3.Response.Builder().request(rawRequest).message("no body").protocol(Protocol.HTTP_1_1).code(401).header(GithubApiService.TWO_FACTOR_HEADER, "required").build();
    Response response = Response.error(ResponseBody.create(null, ""), rawResponse);
    HttpException exception = new HttpException(response);
    when(mGitHubRestClient.getApiService().createNewAuthorization(eq(auth), any(AuthorizationRequest.class))).thenReturn(Single.error(exception));
    mAuthPresenter.login(login, password);
    verify(mAuthView).showTwoFactorAuth();
    // rules for handling 2-factor auth continuation
    final String securityCode = "123456";
    final String gitHubToken = "gitHubToken";
    Authorization authorization = new Authorization();
    authorization.setToken(gitHubToken);
    authorization.setId(1L);
    when(mGitHubRestClient.getApiService().createNewAuthorization(eq(auth), eq(securityCode), any(AuthorizationRequest.class))).thenReturn(Single.just(authorization));
    final String accessToken = "token";
    AccessTokenRequest request = new AccessTokenRequest();
    request.setGithubToken(gitHubToken);
    AccessToken token = new AccessToken();
    token.setAccessToken(accessToken);
    when(mTravisRestClient.getApiService().auth(request)).thenReturn(Single.just(token));
    when(mGitHubRestClient.getApiService().deleteAuthorization(auth, String.valueOf(authorization.getId()))).thenReturn(null);
    mAuthPresenter.twoFactorAuth(securityCode);
    verify(mAuthView, times(2)).hideProgress();
    verify(mAuthView).finishView();
}
Also used : Response(retrofit2.Response) Authorization(com.khmelenko.lab.varis.network.response.Authorization) AuthorizationRequest(com.khmelenko.lab.varis.network.request.AuthorizationRequest) AccessToken(com.khmelenko.lab.varis.network.response.AccessToken) AuthorizationRequest(com.khmelenko.lab.varis.network.request.AuthorizationRequest) Request(okhttp3.Request) AccessTokenRequest(com.khmelenko.lab.varis.network.request.AccessTokenRequest) HttpException(retrofit2.HttpException) AccessTokenRequest(com.khmelenko.lab.varis.network.request.AccessTokenRequest) Test(org.junit.Test)

Example 17 with HttpException

use of com.jakewharton.retrofit2.adapter.rxjava2.HttpException in project Palm300Heroes by nicolite.

the class ExceptionEngine method handleException.

public static APIException handleException(Throwable throwable) {
    throwable.printStackTrace();
    APIException apiException;
    if (throwable instanceof HttpException) {
        HttpException httpException = (HttpException) throwable;
        apiException = new APIException(throwable, httpException.code());
        apiException.setMsg("网络错误");
        return apiException;
    } else if (throwable instanceof JsonParseException || throwable instanceof JSONException || throwable instanceof ParseException || throwable instanceof MalformedJsonException) {
        apiException = new APIException(throwable, PARSE_SERVER_DATA_ERROR);
        apiException.setMsg("解析数据错误");
        return apiException;
    } else if (throwable instanceof ConnectException) {
        apiException = new APIException(throwable, CONNECT_ERROR);
        apiException.setMsg("网络连接失败");
        return apiException;
    } else if (throwable instanceof SocketTimeoutException) {
        apiException = new APIException(throwable, CONNECT_TIME_OUT_ERROR);
        apiException.setMsg("网络连接超时");
        return apiException;
    } else if (throwable instanceof ServerException) {
        ServerException serverException = (ServerException) throwable;
        apiException = new APIException(serverException, serverException.getCode());
        apiException.setMsg(serverException.getMsg());
        return apiException;
    } else {
        apiException = new APIException(throwable, UN_KNOWN_ERROR);
        apiException.setMsg("未知错误");
        return apiException;
    }
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) JSONException(org.json.JSONException) HttpException(retrofit2.HttpException) JsonParseException(com.google.gson.JsonParseException) ParseException(java.text.ParseException) JsonParseException(com.google.gson.JsonParseException) MalformedJsonException(com.google.gson.stream.MalformedJsonException) ConnectException(java.net.ConnectException)

Example 18 with HttpException

use of com.jakewharton.retrofit2.adapter.rxjava2.HttpException in project SmartCampus by Vegen.

the class HttpError method getErrorCode.

public static int getErrorCode(Throwable throwable) {
    if (throwable instanceof HttpException) {
        try {
            HttpException httpException = (HttpException) throwable;
            String errorMsg = httpException.response().errorBody().string();
            HashMap data = new Gson().fromJson(errorMsg, HashMap.class);
            if (data.containsKey("msg")) {
                return ((Double) data.get("code")).intValue();
            } else {
                return -1;
            }
        } catch (IOException e) {
            LogUtils.e(e.getMessage());
        } catch (JsonSyntaxException e) {
            LogUtils.e(e.getMessage());
            return -1;
        } catch (Exception e) {
            return -1;
        }
    }
    return -1;
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) HashMap(java.util.HashMap) Gson(com.google.gson.Gson) HttpException(retrofit2.HttpException) IOException(java.io.IOException) JsonSyntaxException(com.google.gson.JsonSyntaxException) SocketTimeoutException(java.net.SocketTimeoutException) HttpException(retrofit2.HttpException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 19 with HttpException

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

the class ExceptionHandle 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:
            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 = "未知错误";
        return ex;
    }
}
Also used : 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)

Example 20 with HttpException

use of com.jakewharton.retrofit2.adapter.rxjava2.HttpException in project AndroidComponent by funnyzhaov.

the class ExceptionHandle method handleException.

/**
 * 异常的处理
 */
@NonNull
public static Throwable handleException(Throwable e) {
    ResponseThrowable ex;
    if (e instanceof HttpException) {
        HttpException httpException = (HttpException) e;
        ex = new ResponseThrowable(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 ResponseThrowable(resultException, resultException.status);
        ex.message = resultException.message;
        return ex;
    } else if (e instanceof JsonParseException || e instanceof JSONException || e instanceof ParseException) {
        ex = new ResponseThrowable(e, ERROR.PARSE_ERROR);
        ex.message = "解析错误";
        return ex;
    } else if (e instanceof ConnectException) {
        ex = new ResponseThrowable(e, ERROR.NETWORD_ERROR);
        ex.message = "连接失败";
        return ex;
    } else if (e instanceof javax.net.ssl.SSLHandshakeException) {
        ex = new ResponseThrowable(e, ERROR.SSL_ERROR);
        ex.message = "证书验证失败";
        return ex;
    } else if (e instanceof ConnectTimeoutException) {
        ex = new ResponseThrowable(e, ERROR.TIMEOUT_ERROR);
        ex.message = "连接超时";
        return ex;
    } else if (e instanceof java.net.SocketTimeoutException) {
        ex = new ResponseThrowable(e, ERROR.TIMEOUT_ERROR);
        ex.message = "连接超时";
        return ex;
    } else {
        ex = new ResponseThrowable(e, ERROR.UNKNOWN);
        ex.message = "未知错误";
        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) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException) NonNull(android.support.annotation.NonNull)

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