feat: 升级1.0.9,解决异常情况

This commit is contained in:
wangyu 2023-02-01 19:20:40 +08:00
parent a814b9e598
commit b60caa6bd2
7 changed files with 107 additions and 41 deletions

View File

@ -13,7 +13,7 @@
<groupId>group.flyfish</groupId>
<artifactId>rest-proxy</artifactId>
<version>1.0.8</version>
<version>1.0.9</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
@ -22,7 +22,7 @@
<java.version>1.8</java.version>
<commons-collection.version>4.4</commons-collection.version>
<commons.lang.version>2.6</commons.lang.version>
<sdk.version>1.0.8</sdk.version>
<sdk.version>1.0.9</sdk.version>
</properties>
<packaging>pom</packaging>

View File

@ -5,7 +5,7 @@
<parent>
<groupId>group.flyfish</groupId>
<artifactId>rest-proxy</artifactId>
<version>1.0.8</version>
<version>1.0.9</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<groupId>group.flyfish</groupId>
<artifactId>rest-proxy</artifactId>
<version>1.0.8</version>
<version>1.0.9</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -214,4 +214,10 @@ public interface RestConstants {
.with(TypeConstants.BYTE_ARRAY, ResponseType.BINARY)
.build();
// 提示消息
String MSG_THREAD_POOL_EMPTY = "线程池未指定或为空!";
String MSG_IO_ERROR = "发起请求时出现异常!";
String MSG_UNKNOWN_HOST = "未知的请求地址!";
String MSG_REQUEST_ERROR = "请求接口{0}状态异常!代码:{1}";
}

View File

@ -40,12 +40,11 @@ import static group.flyfish.rest.constants.RestConstants.DEFAULT_EXECUTOR;
* 6. 新增单例httpClient模式复用连接让应用更高效
*/
@Slf4j
final class DefaultRestClient implements RestClient {
final class DefaultRestClient extends RestErrorHandler implements RestClient {
private final HttpRequestBase request;
private boolean async = false;
private Consumer<HttpEntity> consumer;
private Consumer<RestClientException> errorConsumer;
private ExecutorService executorService;
private ResponseType responseType = ResponseType.NORMAL;
private Class<?> resultClass;
@ -85,18 +84,6 @@ final class DefaultRestClient implements RestClient {
return this;
}
/**
* 错误处理
*
* @param e 异常
*/
private void handleError(RestClientException e) {
if (null != errorConsumer) {
errorConsumer.accept(e);
} else {
throw e;
}
}
/**
* 设置响应类型
@ -155,7 +142,7 @@ final class DefaultRestClient implements RestClient {
this.consumer = consumer;
if (this.async) {
if (this.executorService == null) {
handleError(new RestClientException("线程池未指定或为空!"));
handleError(RestConstants.MSG_THREAD_POOL_EMPTY);
}
this.executorService.submit(this::executeSafety);
} else {
@ -202,7 +189,7 @@ final class DefaultRestClient implements RestClient {
try {
execute();
} catch (IOException e) {
handleError(new RestClientException(e.getMessage(), e, null));
handleError(RestConstants.MSG_IO_ERROR, e);
}
}
@ -287,21 +274,14 @@ final class DefaultRestClient implements RestClient {
log.info("【Rest Invoke】{} {}", request.getMethod(), request.getURI());
try (CloseableHttpResponse response = clientProvider.getClient().execute(request)) {
StatusLine statusLine = response.getStatusLine();
HttpEntity entity = response.getEntity();
if (HttpStatus.valueOf(statusLine.getStatusCode()).isError()) {
int requestCode = statusLine.getStatusCode();
log.error(request.getURI() + "接口调用失败code:" + requestCode);
handleError(new RestClientException("网络请求状态异常!代码:" + requestCode, null, statusLine));
handleError(request.getURI(), statusLine.getStatusCode(), handleEntity(entity));
} else {
HttpEntity entity = response.getEntity();
if (consumer != null) {
consumer.accept(entity);
return null;
}
return resolveResponse(entity);
return handleEntity(entity);
}
} catch (UnknownHostException e) {
handleError(new RestClientException(e.getMessage(), e, null));
log.error("未知的请求地址!");
handleError(RestConstants.MSG_UNKNOWN_HOST, e);
} finally {
request.releaseConnection();
}
@ -318,6 +298,24 @@ final class DefaultRestClient implements RestClient {
return RestConstants.RESPONSE_TYPE_MAP.getOrDefault(clazz.getName(), ResponseType.OBJECT);
}
/**
* 处理响应体
*
* @param entity 响应体
* @return 结果
* @throws IOException 异常
*/
private <T> T handleEntity(HttpEntity entity) throws IOException {
if (null == entity) {
return null;
}
if (consumer != null) {
consumer.accept(entity);
return null;
}
return resolveResponse(entity);
}
/**
* 解析结果
*

View File

@ -0,0 +1,64 @@
package group.flyfish.rest.core.client;
import group.flyfish.rest.constants.RestConstants;
import group.flyfish.rest.core.exception.RestClientException;
import lombok.extern.slf4j.Slf4j;
import java.net.URI;
import java.text.MessageFormat;
import java.util.function.Consumer;
/**
* rest请求错误处理器
*
* @author wangyu
*/
@Slf4j
public abstract class RestErrorHandler {
protected Consumer<RestClientException> errorConsumer;
/**
* 错误处理
*
* @param e 异常
*/
private void handleError(RestClientException e) {
log.error(e.getMessage());
if (null != errorConsumer) {
errorConsumer.accept(e);
} else {
throw e;
}
}
/**
* 处理常规文本异常
*
* @param message 消息
*/
protected void handleError(String message) {
handleError(new RestClientException(message));
}
/**
* 处理具体发生的异常
*
* @param message 信息
* @param cause 造成的异常
*/
protected void handleError(String message, Exception cause) {
handleError(new RestClientException(message + cause.getMessage(), cause));
}
/**
* 处理请求异常
*
* @param status 状态码
* @param data 响应数据
*/
protected void handleError(URI uri, int status, Object data) {
String message = MessageFormat.format(RestConstants.MSG_REQUEST_ERROR, uri, status);
handleError(new RestClientException(message, status, data));
}
}

View File

@ -1,13 +1,19 @@
package group.flyfish.rest.core.exception;
import lombok.Getter;
/**
* 异常类用于包装异常
*/
public class RestClientException extends RuntimeException {
private static final long serialVersionUID = 4741281547788724661L;
@Getter
private Exception nested;
@Getter
private int statusCode;
private Object bind;
public RestClientException(String message, Exception nested) {
@ -15,9 +21,9 @@ public class RestClientException extends RuntimeException {
this.nested = nested;
}
public RestClientException(String message, Exception nested, Object bind) {
public RestClientException(String message, int statusCode, Object bind) {
super(message);
this.nested = nested;
this.statusCode = statusCode;
this.bind = bind;
}
@ -29,12 +35,4 @@ public class RestClientException extends RuntimeException {
public <T> T getBind() {
return (T) bind;
}
public void setBind(Object bind) {
this.bind = bind;
}
public Exception getNested() {
return nested;
}
}