use of com.dtflys.forest.exceptions.ForestNetworkException in project forest by dromara.
the class SyncHttpclientRequestSender method sendRequest.
@Override
public void sendRequest(ForestRequest request, AbstractHttpExecutor executor, HttpclientResponseHandler responseHandler, HttpUriRequest httpRequest, LifeCycleHandler lifeCycleHandler, CookieStore cookieStore, Date startDate) {
HttpResponse httpResponse = null;
ForestResponse response = null;
HttpContext httpContext = new BasicHttpContext();
httpContext.setAttribute("REQUEST", request);
HttpClientContext httpClientContext = HttpClientContext.adapt(httpContext);
client = getHttpClient(cookieStore);
ForestResponseFactory forestResponseFactory = new HttpclientForestResponseFactory();
try {
logRequest(request.getCurrentRetryCount(), (HttpRequestBase) httpRequest);
httpResponse = client.execute(httpRequest, httpClientContext);
} catch (Throwable e) {
httpRequest.abort();
response = forestResponseFactory.createResponse(request, httpResponse, lifeCycleHandler, e, startDate);
ForestRetryException retryException = new ForestRetryException(e, request, request.getRetryCount(), request.getCurrentRetryCount());
try {
request.canRetry(response, retryException);
} catch (Throwable throwable) {
response = forestResponseFactory.createResponse(request, httpResponse, lifeCycleHandler, throwable, startDate);
lifeCycleHandler.handleSyncWithException(request, response, e);
return;
}
response = forestResponseFactory.createResponse(request, httpResponse, lifeCycleHandler, null, startDate);
logResponse(response);
executor.execute(lifeCycleHandler);
return;
} finally {
if (response == null) {
response = forestResponseFactory.createResponse(request, httpResponse, lifeCycleHandler, null, startDate);
}
logResponse(response);
}
response = forestResponseFactory.createResponse(request, httpResponse, lifeCycleHandler, null, startDate);
// 检查是否重试
ForestRetryException retryEx = request.canRetry(response);
if (retryEx != null && retryEx.isNeedRetry() && !retryEx.isMaxRetryCountReached()) {
executor.execute(lifeCycleHandler);
return;
}
// 检查响应是否失败
if (response.isError()) {
ForestNetworkException networkException = new ForestNetworkException("", response.getStatusCode(), response);
ForestRetryException retryException = new ForestRetryException(networkException, request, request.getRetryCount(), request.getCurrentRetryCount());
try {
request.canRetry(response, retryException);
} catch (Throwable throwable) {
responseHandler.handleSync(httpResponse, response);
return;
}
executor.execute(lifeCycleHandler);
return;
}
try {
lifeCycleHandler.handleSaveCookie(request, getCookiesFromHttpCookieStore(cookieStore));
responseHandler.handleSync(httpResponse, response);
} catch (Exception ex) {
if (ex instanceof ForestRuntimeException) {
throw ex;
} else {
throw new ForestRuntimeException(ex);
}
}
}
use of com.dtflys.forest.exceptions.ForestNetworkException in project forest by dromara.
the class OkHttp3Executor method retryOrDoError.
private void retryOrDoError(ForestResponse response, Response okResponse, OkHttp3ResponseFuture future, LifeCycleHandler lifeCycleHandler, int retryCount) {
ForestNetworkException networkException = new ForestNetworkException(okResponse.message(), okResponse.code(), response);
ForestRetryException retryException = new ForestRetryException(networkException, request, request.getRetryCount(), retryCount);
try {
request.canRetry(response, retryException);
} catch (Throwable throwable) {
if (future != null) {
future.failed(new ForestNetworkException(okResponse.message(), okResponse.code(), response));
}
logResponse(response);
okHttp3ResponseHandler.handleSync(okResponse, response);
return;
}
execute(lifeCycleHandler, retryCount + 1);
}
use of com.dtflys.forest.exceptions.ForestNetworkException in project forest by dromara.
the class TestAsyncGetClient method testAsyncVarParamGetError_404.
@Test
public void testAsyncVarParamGetError_404() throws InterruptedException {
server.enqueue(new MockResponse().setResponseCode(404).setBody(EXPECTED));
final AtomicBoolean success = new AtomicBoolean(false);
final AtomicBoolean error = new AtomicBoolean(false);
CountDownLatch latch = new CountDownLatch(1);
Future<String> future = getClient.asyncVarParamGet("error param", (data, request, response) -> {
error.set(false);
success.set(true);
latch.countDown();
}, (ex, request, response) -> {
error.set(true);
success.set(false);
assertTrue(ex instanceof ForestNetworkException);
int statusCode = ((ForestNetworkException) ex).getStatusCode();
log.error("status code = " + statusCode);
assertEquals(404, statusCode);
latch.countDown();
});
log.info("send async get request");
assertFalse(error.get());
assertThat(future).isNotNull();
latch.await(5, TimeUnit.SECONDS);
assertFalse(success.get());
assertTrue(error.get());
}
use of com.dtflys.forest.exceptions.ForestNetworkException in project forest by dromara.
the class TestAsyncGetClient method testAsyncVarParamGetError_400.
@Test
public void testAsyncVarParamGetError_400() throws InterruptedException {
server.enqueue(new MockResponse().setResponseCode(400).setBody(EXPECTED));
final AtomicBoolean success = new AtomicBoolean(false);
final AtomicBoolean error = new AtomicBoolean(false);
CountDownLatch latch = new CountDownLatch(1);
Future<String> future = getClient.asyncVarParamGet("error param", (data, request, response) -> {
error.set(false);
success.set(true);
latch.countDown();
}, (ex, request, response) -> {
error.set(true);
success.set(false);
assertTrue(ex instanceof ForestNetworkException);
int statusCode = ((ForestNetworkException) ex).getStatusCode();
log.error("status code = " + statusCode);
assertEquals(400, statusCode);
latch.countDown();
});
log.info("send async get request");
assertFalse(error.get());
assertThat(future).isNotNull();
latch.await(5, TimeUnit.SECONDS);
assertFalse(success.get());
assertTrue(error.get());
}
use of com.dtflys.forest.exceptions.ForestNetworkException in project forest by dromara.
the class TestErrorClient method testErrorGet3.
@Test
public void testErrorGet3() {
server.enqueue(new MockResponse().setResponseCode(500).setBody(EXPECTED));
boolean hasError = false;
try {
getClient.errorGet3();
} catch (ForestNetworkException ex) {
ex.printStackTrace();
hasError = true;
assertThat(ex.getStatusCode()).isEqualTo(500);
assertThat(ex.getResponse()).isNotNull().extracting(ForestResponse::getContent).isEqualTo(EXPECTED);
}
assertThat(hasError).isTrue();
}
Aggregations