diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/ErrorDefinition.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/ErrorDefinition.java index 9b974a80..6b929b7a 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/ErrorDefinition.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/ErrorDefinition.java @@ -7,9 +7,21 @@ import lombok.Data; */ @Data public class ErrorDefinition { + /** + * 接口名 + */ private String name; + /** + * 版本号 + */ private String version; + /** + * 服务名 + */ private String serviceId; + /** + * 错误内容 + */ private String errorMsg; } diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/DefaultServiceErrorManager.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/DefaultServiceErrorManager.java index 56d1bddf..a3e0e8ed 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/DefaultServiceErrorManager.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/DefaultServiceErrorManager.java @@ -23,7 +23,12 @@ public class DefaultServiceErrorManager implements ServiceErrorManager { } @Override - public void saveError(ErrorDefinition errorDefinition) { + public void saveBizError(ErrorDefinition errorDefinition) { + + } + + @Override + public void saveUnknownError(ErrorDefinition errorDefinition) { boolean hasCapacity = store.size() < ApiConfig.getInstance().getStoreErrorCapacity(); // 这里还可以做其它事情,比如错误量到达一定数目后,自动发送邮件/微信给开发人员,方便及时获取异常情况 String id = this.buildId(errorDefinition); diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/ServiceErrorManager.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/ServiceErrorManager.java index 8528d4b7..9e587507 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/ServiceErrorManager.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/ServiceErrorManager.java @@ -11,10 +11,16 @@ import java.util.Collection; public interface ServiceErrorManager { /** - * 保存错误信息 + * 保存业务错误,一般由开发人员自己throw的异常 * @param errorDefinition */ - void saveError(ErrorDefinition errorDefinition); + void saveBizError(ErrorDefinition errorDefinition); + + /** + * 保存未知的错误信息 + * @param errorDefinition + */ + void saveUnknownError(ErrorDefinition errorDefinition); /** * 清除日志 diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/result/BaseExecutorAdapter.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/result/BaseExecutorAdapter.java index 780fc514..7665577c 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/result/BaseExecutorAdapter.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/result/BaseExecutorAdapter.java @@ -78,11 +78,12 @@ public abstract class BaseExecutorAdapter implements ResultExecutor jsonObjectService.put(GATEWAY_MSG_NAME, SUCCESS_META.getError().getMsg()); } else if (responseStatus == SopConstants.BIZ_ERROR_STATUS) { // 如果是业务出错 + this.storeError(request, ErrorType.BIZ); jsonObjectService = JSON.parseObject(serviceResult); jsonObjectService.put(GATEWAY_CODE_NAME, ISP_BIZ_ERROR.getCode()); jsonObjectService.put(GATEWAY_MSG_NAME, ISP_BIZ_ERROR.getError().getMsg()); } else { - this.storeError(request); + this.storeError(request, ErrorType.UNKNOWN); // 微服务端有可能返回500错误 // {"path":"/book/getBook3","error":"Internal Server Error","message":"id不能为空","timestamp":"2019-02-13T07:41:00.495+0000","status":500} jsonObjectService = new JSONObject(); @@ -97,13 +98,18 @@ public abstract class BaseExecutorAdapter implements ResultExecutor * * @param request */ - protected void storeError(T request) { + protected void storeError(T request, ErrorType errorType) { ApiInfo apiInfo = this.getApiInfo(request); String errorMsg = this.getResponseErrorMessage(request); ErrorDefinition errorDefinition = new ErrorDefinition(); BeanUtils.copyProperties(apiInfo, errorDefinition); errorDefinition.setErrorMsg(errorMsg); - ApiConfig.getInstance().getServiceErrorManager().saveError(errorDefinition); + if (errorType == ErrorType.UNKNOWN) { + ApiConfig.getInstance().getServiceErrorManager().saveUnknownError(errorDefinition); + } + if (errorType == ErrorType.BIZ) { + ApiConfig.getInstance().getServiceErrorManager().saveBizError(errorDefinition); + } } @@ -223,4 +229,7 @@ public abstract class BaseExecutorAdapter implements ResultExecutor private BaseRouteDefinition baseRouteDefinition; } + enum ErrorType { + UNKNOWN, BIZ + } }