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();
}
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;
}
}
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;
}
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;
}
}
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;
}
}
Aggregations