diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/aspect/BaseRequestLogAspect.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/aspect/BaseRequestLogAspect.java index bce71012a1..7f9399c5e1 100644 --- a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/aspect/BaseRequestLogAspect.java +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/aspect/BaseRequestLogAspect.java @@ -16,6 +16,12 @@ import java.time.LocalDateTime; /** * 日志切面 + * + * 异常拦截: + * 会将错误信息都放到internalMsg中 + * 如果是要提示给用户的异常,则原样转化为result,return + * 如果是内部异常,则转化为服务器开小差,然后return + * * @Author wxz * @Description **/ @@ -41,45 +47,68 @@ public abstract class BaseRequestLogAspect { protected Object proceed(ProceedingJoinPoint point, HttpServletRequest request) throws Throwable { String requestURI = request.getRequestURI(); - long id = Thread.currentThread().getId(); - Object result = null; - Exception exception = null; + long threadId = Thread.currentThread().getId(); + Object result; LocalDateTime startTime = LocalDateTime.now(); try { Object[] args = point.getArgs(); - log.info(">>>>>>>>请求信息>>>>>>>>:线程ID:{},url:{},请求参数:{}", id, requestURI, objectsToString(args)); + log.info(">>>>>>>>请求信息>>>>>>>>:线程ID:{},url:{},请求参数:{}", threadId, requestURI, objectsToString(args)); result = point.proceed(); + resultInfoLog(threadId, getExecPeriod(startTime), result); } catch (RenException e) { - exception = e; result = handleRenException(e); + resultErrorLog(threadId, getExecPeriod(startTime), result, e.getMsg(), ExceptionUtils.getErrorStackTrace(e)); } catch (ValidateException e) { - exception = e; result = handleValidateException(e); + resultErrorLog(threadId, getExecPeriod(startTime), result, e.getMsg(), ExceptionUtils.getErrorStackTrace(e)); } catch (DuplicateKeyException e) { - exception = e; result = handlerDuplicateKeyException(e); + resultErrorLog(threadId, getExecPeriod(startTime), result, e.getMessage(), ExceptionUtils.getErrorStackTrace(e)); } catch (RuntimeException re) { - exception = re; result = handlerRuntimeException(re); + resultErrorLog(threadId, getExecPeriod(startTime), result, re.getMessage(), ExceptionUtils.getErrorStackTrace(re)); } catch (Exception e) { - exception = e; result = handlerException(e); - } finally { - LocalDateTime endTime = LocalDateTime.now(); - long execTimeMillis = Duration.between(startTime, endTime).toMillis(); - - if (exception != null) { - log.error("<<<<<<<<异常响应<<<<<<<<:线程ID:{},执行时长:{}ms, 响应数据:{}, 异常信息:{}", - id, execTimeMillis, result == null ? result : result.toString(), ExceptionUtils.getErrorStackTrace(exception)); - } else { - log.info("<<<<<<<<正常响应<<<<<<<<:线程ID:{},执行时长:{}ms, 响应数据:{}", - id, execTimeMillis, result == null ? result : result.toString()); - } + resultErrorLog(threadId, getExecPeriod(startTime), result, e.getMessage(), ExceptionUtils.getErrorStackTrace(e)); } return result; } + /** + * info日志 + * @param threadId + * @param execTimeMillis + * @param result + */ + private void resultInfoLog(Long threadId, Long execTimeMillis, Object result) { + log.info("<<<<<<<<正常响应<<<<<<<<:线程ID:{},执行时长:{}ms, 响应数据:{}", + threadId, execTimeMillis, result == null ? result : result.toString()); + } + + /** + * 异常信息 + * @param threadId + * @param execTimeMillis + * @param result + * @param exceptionMsg + * @param exceptionDetail + */ + private void resultErrorLog(Long threadId, Long execTimeMillis, Object result, String exceptionMsg, String exceptionDetail) { + log.error("<<<<<<<<异常响应<<<<<<<<:线程ID:{},执行时长:{}ms, 响应数据:{}, 异常信息:{}, 堆栈信息:{}", + threadId, execTimeMillis, result == null ? result : result.toString(), exceptionMsg, exceptionDetail); + } + + /** + * 计算执行周期 + * @param startTime + * @return + */ + private Long getExecPeriod(LocalDateTime startTime) { + LocalDateTime endTime = LocalDateTime.now(); + return Duration.between(startTime, endTime).toMillis(); + } + /** * 处理Exception * @param e @@ -87,7 +116,7 @@ public abstract class BaseRequestLogAspect { */ private Result handlerException(Exception e) { Result result=new Result().error(); - result.setData(e.getMessage()); + result.setInternalMsg(e.getMessage()); return result; } @@ -105,7 +134,7 @@ public abstract class BaseRequestLogAspect { */ private Result handlerRuntimeException(RuntimeException ex) { Result result=new Result().error(); - result.setData(ex.getMessage()); + result.setInternalMsg(ex.getMessage()); return result; } @@ -128,12 +157,14 @@ public abstract class BaseRequestLogAspect { */ private Result handleRenException(RenException e) { if (e.getCode() > 8000) { + // 原样返回 Result result=new Result().error(e.getCode()); - result.setData(e.getMsg()); + result.setInternalMsg(e.getMsg()); return result; } + // 转化成服务器开小差... Result result=new Result().error(); - result.setData(e.getMsg()); + result.setInternalMsg(e.getMsg()); //result.setMsg(e.getMsg()); return result; } diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/RenException.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/RenException.java index b775eb8f9a..36d9a85418 100644 --- a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/RenException.java +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/RenException.java @@ -24,11 +24,7 @@ public class RenException extends RuntimeException { private String msg; public RenException(int code) { - this.code = code; - this.msg = EpmetErrorCode.getMsg(code); - if (StringUtils.isBlank(this.msg)) { - this.msg = MessageUtils.getMessage(code); - } + this(code, ""); } public RenException(int code, String... params) { @@ -55,6 +51,7 @@ public class RenException extends RuntimeException { } public RenException(int code, String msg) { + super(msg); this.code = code; if (StringUtils.isBlank(msg)) { this.msg = EpmetErrorCode.getMsg(code); diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/Result.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/Result.java index 22cf316326..b3de990d05 100644 --- a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/Result.java +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/Result.java @@ -37,6 +37,12 @@ public class Result implements Serializable { */ @ApiModelProperty(value = "消息内容") private String msg = "success"; + + /** + * 内部信息 + */ + private String internalMsg; + /** * 响应数据 */ @@ -126,6 +132,14 @@ public class Result implements Serializable { this.data = data; } + public String getInternalMsg() { + return internalMsg; + } + + public void setInternalMsg(String internalMsg) { + this.internalMsg = internalMsg; + } + @Override public String toString() { return JSON.toJSONString(this); diff --git a/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/service/impl/AgencyServiceImpl.java b/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/service/impl/AgencyServiceImpl.java index 46b5bc90d1..1393804624 100644 --- a/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/service/impl/AgencyServiceImpl.java +++ b/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/service/impl/AgencyServiceImpl.java @@ -260,7 +260,8 @@ public class AgencyServiceImpl implements AgencyService { CustomerAgencyDTO rootAgencyExists = customerAgencyDao.getCustomerRootAgency(form.getCustomerId()); if (rootAgencyExists != null) { - throw new RenException(EpmetErrorCode.OPER_ADD_CUSTOMER_ROOT_AGENCY_EXISTS.getCode()); + throw new RenException(EpmetErrorCode.OPER_ADD_CUSTOMER_ROOT_AGENCY_EXISTS.getCode(), + EpmetErrorCode.OPER_ADD_CUSTOMER_ROOT_AGENCY_EXISTS.getMsg()); } CustomerAgencyEntity entity = ConvertUtils.sourceToTarget(form, CustomerAgencyEntity.class); diff --git a/epmet-module/oper-crm/oper-crm-server/src/main/java/com/epmet/controller/CustomerController.java b/epmet-module/oper-crm/oper-crm-server/src/main/java/com/epmet/controller/CustomerController.java index be507662b6..1caf84c054 100644 --- a/epmet-module/oper-crm/oper-crm-server/src/main/java/com/epmet/controller/CustomerController.java +++ b/epmet-module/oper-crm/oper-crm-server/src/main/java/com/epmet/controller/CustomerController.java @@ -192,8 +192,7 @@ public class CustomerController { public Result> addRootAgency(@RequestBody AddRootAgencyFormDTO form) { Result> result = govOrgFeignClient.addRootAgency(form); if (!result.success()) { - throw new RenException(EpmetErrorCode.OPER_ADD_CUSTOMER_ROOT_AGENCY_ERROR.getCode(), - EpmetErrorCode.OPER_ADD_CUSTOMER_ROOT_AGENCY_ERROR.getMsg().concat(",错误信息:").concat(result.getMsg())); + throw new RenException(EpmetErrorCode.OPER_ADD_CUSTOMER_ROOT_AGENCY_ERROR.getCode(), result.getInternalMsg()); } return result; }