Browse Source

1.调整异常类和异常拦截BaseRequestLogAspect,将msg和InternalMsg区分开来,允许手动指定internalMsg和msg

dev_shibei_match
wxz 5 years ago
parent
commit
2a971221f7
  1. 16
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/aspect/BaseRequestLogAspect.java
  2. 96
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/RenException.java
  3. 5
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/RenExceptionHandler.java
  4. 4
      epmet-gateway/src/main/java/com/epmet/auth/ExternalAuthProcessor.java
  5. 4
      epmet-gateway/src/main/java/com/epmet/auth/InternalAuthProcessor.java
  6. 5
      epmet-module/gov-issue/gov-issue-server/src/main/java/com/epmet/service/impl/IssueServiceImpl.java

16
epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/aspect/BaseRequestLogAspect.java

@ -66,7 +66,7 @@ public abstract class BaseRequestLogAspect {
resultInfoLog(transactionSerial, getExecPeriod(startTime), result);
} catch (RenException e) {
result = handleRenException(e);
resultErrorLog(transactionSerial, getExecPeriod(startTime), result, e.getMsg(), ExceptionUtils.getErrorStackTrace(e));
resultErrorLog(transactionSerial, getExecPeriod(startTime), result, e.getInternalMsg(), ExceptionUtils.getErrorStackTrace(e));
} catch (ValidateException e) {
result = handleValidateException(e);
resultErrorLog(transactionSerial, getExecPeriod(startTime), result, e.getMsg(), ExceptionUtils.getErrorStackTrace(e));
@ -174,14 +174,20 @@ public abstract class BaseRequestLogAspect {
*/
private Result handleRenException(RenException e) {
if (e.getCode() > 8000) {
// 原样返回
Result result=new Result().error(e.getCode());
result.setInternalMsg(e.getMsg());
Result result;
if (StringUtils.isNotBlank(e.getMsg())) {
// 抛出异常的时候填写了自定义显示信息,把显示信息返回
result = new Result().error(e.getCode(), e.getMsg());
} else {
// 没有填写显示信息,则根据code找固定的显示信息
result = new Result().error(e.getCode());
}
result.setInternalMsg(e.getInternalMsg());
return result;
}
// 转化成服务器开小差...
Result result=new Result().error();
result.setInternalMsg(e.getMsg());
result.setInternalMsg(e.getInternalMsg());
//result.setMsg(e.getMsg());
return result;
}

96
epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/RenException.java

@ -21,26 +21,67 @@ import org.apache.commons.lang3.StringUtils;
public class RenException extends RuntimeException {
private static final long serialVersionUID = 1L;
private int code;
/**
* 显示给客户的消息
*/
private String msg;
/**
* 内部消息用于服务之间传递错误信息排错用
*/
private String internalMsg;
public RenException(int code) {
this(code, "");
}
public RenException(int code, String internalMsg) {
super(internalMsg);
this.code = code;
if (StringUtils.isBlank(internalMsg)) {
this.internalMsg = EpmetErrorCode.getMsg(code);
if (StringUtils.isBlank(this.internalMsg)) {
this.internalMsg = MessageUtils.getMessage(code, internalMsg);
}
} else {
this.internalMsg = internalMsg;
}
}
public RenException(int code, String internalMsg, String msg) {
this(code, internalMsg);
this.msg = msg;
}
public RenException(String internalMsg) {
super(internalMsg);
// this.code = ErrorCode.INTERNAL_SERVER_ERROR;
this.code = EpmetErrorCode.SERVER_ERROR.getCode();
this.internalMsg = internalMsg;
}
public RenException(String internalMsg, String msg) {
this(internalMsg);
this.msg = msg;
}
public RenException(int code, String... params) {
this.code = code;
this.msg = EpmetErrorCode.getMsg(code);
if (StringUtils.isBlank(this.msg)) {
this.msg = MessageUtils.getMessage(code, params);
this.internalMsg = EpmetErrorCode.getMsg(code);
if (StringUtils.isBlank(this.internalMsg)) {
this.internalMsg = MessageUtils.getMessage(code, params);
}
}
public RenException(int code, Throwable e) {
super(e);
this.code = code;
this.msg = EpmetErrorCode.getMsg(code);
if (StringUtils.isBlank(this.msg)) {
this.msg = MessageUtils.getMessage(code);
this.internalMsg = EpmetErrorCode.getMsg(code);
if (StringUtils.isBlank(this.internalMsg)) {
this.internalMsg = MessageUtils.getMessage(code);
}
}
@ -50,40 +91,18 @@ 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);
if (StringUtils.isBlank(this.msg)) {
this.msg = MessageUtils.getMessage(code, msg);
}
} else {
this.msg = msg;
}
}
public RenException(String msg) {
super(msg);
// this.code = ErrorCode.INTERNAL_SERVER_ERROR;
public RenException(String internalMsg, Throwable e) {
super(internalMsg, e);
this.code = EpmetErrorCode.SERVER_ERROR.getCode();
this.msg = msg;
this.internalMsg = internalMsg;
}
public RenException(String msg, Throwable e) {
super(msg, e);
// this.code = ErrorCode.INTERNAL_SERVER_ERROR;
this.code = EpmetErrorCode.SERVER_ERROR.getCode();
this.msg = msg;
public String getInternalMsg() {
return internalMsg;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
public void setInternalMsg(String internalMsg) {
this.internalMsg = internalMsg;
}
public int getCode() {
@ -94,4 +113,11 @@ public class RenException extends RuntimeException {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}

5
epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/RenExceptionHandler.java

@ -24,7 +24,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
@ -56,12 +55,12 @@ public class RenExceptionHandler {
public Result handleRRException(RenException ex){
if (ex.getCode() > 8000) {
Result result=new Result().error(ex.getCode());
result.setData(ex.getMsg());
result.setData(ex.getInternalMsg());
return result;
}
logger.error(ExceptionUtils.getErrorStackTrace(ex));
Result result=new Result().error();
result.setData(ex.getMsg());
result.setData(ex.getInternalMsg());
return result;
// return new Result().error();
}

4
epmet-gateway/src/main/java/com/epmet/auth/ExternalAuthProcessor.java

@ -3,7 +3,6 @@ package com.epmet.auth;
import com.epmet.commons.tools.exception.EpmetErrorCode;
import com.epmet.commons.tools.exception.ExceptionUtils;
import com.epmet.commons.tools.exception.RenException;
import com.epmet.commons.tools.utils.Result;
import com.epmet.filter.CpProperty;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
@ -15,7 +14,6 @@ import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
/**
* 外部应用认证
@ -93,7 +91,7 @@ public class ExternalAuthProcessor extends AuthProcessor {
}
} catch (RenException e) {
//return response(exchange, new Result<>().error(e.getCode(), e.getMsg()));
throw new RenException(e.getCode(),e.getMsg());
throw new RenException(e.getCode(),e.getInternalMsg());
} catch (Exception e) {
logger.error("外部应用请求认证发生未知错误:" + ExceptionUtils.getErrorStackTrace(e));
//return response(exchange, new Result<>().error("外部应用请求认证发生未知错误"));

4
epmet-gateway/src/main/java/com/epmet/auth/InternalAuthProcessor.java

@ -54,7 +54,7 @@ public class InternalAuthProcessor extends AuthProcessor {
baseTokenDto = getBaseTokenDto(token, jwtTokenUtils);
}catch(RenException e){
//return response(exchange,new Result<>().error(e.getCode(),e.getMsg()));
throw new RenException(e.getCode(), e.getMsg());
throw new RenException(e.getCode(), e.getInternalMsg());
}
}else{
baseTokenDto = null;
@ -97,7 +97,7 @@ public class InternalAuthProcessor extends AuthProcessor {
validateTokenDto(baseTokenDto, token);
} catch (RenException e) {
//return response(exchange, new Result<>().error(e.getCode(), e.getMsg()));
throw new RenException(e.getCode(), e.getMsg());
throw new RenException(e.getCode(), e.getInternalMsg());
}
}

5
epmet-module/gov-issue/gov-issue-server/src/main/java/com/epmet/service/impl/IssueServiceImpl.java

@ -31,7 +31,6 @@ import com.epmet.dto.result.*;
import com.epmet.entity.IssueEntity;
import com.epmet.entity.IssueProcessEntity;
import com.epmet.entity.IssueProjectRelationEntity;
import com.epmet.entity.IssueVoteStatisticalEntity;
import com.epmet.feign.*;
import com.epmet.redis.GovIssueRedis;
import com.epmet.redis.IssueVoteDetailRedis;
@ -440,7 +439,7 @@ public class IssueServiceImpl extends BaseServiceImpl<IssueDao, IssueEntity> imp
try {
issueVoteStatisticalService.syncVotingCacheToDbByParams(formDTO.getIssueId(),entity.getGridId(),null);
}catch (RenException e){
logger.error(e.getMsg());
logger.error(e.getInternalMsg());
}
}
@ -589,7 +588,7 @@ public class IssueServiceImpl extends BaseServiceImpl<IssueDao, IssueEntity> imp
try{
issueVoteStatisticalService.syncVotingCacheToDbByParams(formDTO.getIssueId(),entity.getGridId(),null);
}catch(RenException e){
logger.error(e.getMsg());
logger.error(e.getInternalMsg());
}
}

Loading…
Cancel
Save