fix: 异常消息拼接问题处理
This commit is contained in:
parent
0cf464e549
commit
98f25179d3
@ -131,4 +131,13 @@ public class ResultUtil<T> {
|
||||
return this.resultMessage;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 返回失败
|
||||
*
|
||||
* @return 消息
|
||||
*/
|
||||
public static <T> ResultMessage<T> error() {
|
||||
return new ResultUtil<T>().setErrorMsg(ResultCode.ERROR);
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package cn.lili.common.exception;
|
||||
|
||||
import cn.hutool.core.text.CharSequenceUtil;
|
||||
import cn.lili.common.enums.ResultCode;
|
||||
import cn.lili.common.enums.ResultUtil;
|
||||
import cn.lili.common.vo.ResultMessage;
|
||||
@ -42,26 +43,21 @@ public class GlobalControllerExceptionHandler {
|
||||
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
|
||||
public ResultMessage<Object> handleServiceException(HttpServletRequest request, final Exception e, HttpServletResponse response) {
|
||||
|
||||
|
||||
//如果是自定义异常,则获取异常,返回自定义错误消息
|
||||
if (e instanceof ServiceException) {
|
||||
|
||||
ServiceException serviceException = ((ServiceException) e);
|
||||
ResultCode resultCode = serviceException.getResultCode();
|
||||
|
||||
Integer code = null;
|
||||
String message = null;
|
||||
Integer code = resultCode.code();
|
||||
String message = resultCode.message();
|
||||
|
||||
if (resultCode != null) {
|
||||
code = resultCode.code();
|
||||
message = resultCode.message();
|
||||
}
|
||||
//如果有扩展消息,则输出异常中,跟随补充异常
|
||||
if (!serviceException.getMsg().equals(ServiceException.DEFAULT_MESSAGE)) {
|
||||
message += ":" + serviceException.getMsg();
|
||||
if (message != null) {
|
||||
message = appendErrorMessage(message, serviceException.getMsg());
|
||||
}
|
||||
|
||||
// 对一些特殊异常处理,不再打印error级别的日志
|
||||
assert serviceException.getResultCode() != null;
|
||||
if (serviceException.getResultCode().equals(ResultCode.DEMO_SITE_EXCEPTION)) {
|
||||
log.debug("[DEMO_SITE_EXCEPTION]:{}", serviceException.getResultCode().message(), e);
|
||||
return ResultUtil.error(code, message);
|
||||
@ -103,7 +99,30 @@ public class GlobalControllerExceptionHandler {
|
||||
|
||||
log.error("全局异常[RuntimeException]:", e);
|
||||
|
||||
return ResultUtil.error(ResultCode.ERROR);
|
||||
// 检查异常链是否包含 ServiceException
|
||||
ServiceException serviceException = findServiceException(e);
|
||||
|
||||
if (serviceException != null) {
|
||||
ResultCode resultCode = serviceException.getResultCode();
|
||||
Integer code = resultCode.code();
|
||||
String message = resultCode.message();
|
||||
if (message != null) {
|
||||
message = appendErrorMessage(message, serviceException.getMsg());
|
||||
}
|
||||
return ResultUtil.error(code, message);
|
||||
}
|
||||
return ResultUtil.error();
|
||||
}
|
||||
|
||||
// 遍历异常链,查找 ServiceException
|
||||
private ServiceException findServiceException(Throwable ex) {
|
||||
while (ex != null) {
|
||||
if (ex instanceof ServiceException) {
|
||||
return (ServiceException) ex;
|
||||
}
|
||||
ex = ex.getCause();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// /**
|
||||
@ -168,4 +187,41 @@ public class GlobalControllerExceptionHandler {
|
||||
ConstraintViolationException exception = (ConstraintViolationException) e;
|
||||
return ResultUtil.error(ResultCode.PARAMS_ERROR.code(), exception.getMessage());
|
||||
}
|
||||
/**
|
||||
* 拼接错误消息
|
||||
*
|
||||
* @param message 原始消息
|
||||
* @param appendMessage 需要拼接的消息
|
||||
* @return 拼接后的消息
|
||||
*/
|
||||
private String appendErrorMessage(String message, String appendMessage) {
|
||||
|
||||
//这里的代码看起来有点乱,简单解释一下
|
||||
//场景1:服务A,服务B=》
|
||||
// 服务A调用服务B=》
|
||||
// 服务B抛出异常{扩展消息},拼接后成为{默认消息}:{扩展消息}
|
||||
// 异常被服务A捕获=》
|
||||
// 最终消息拼接过程中,当前方法体参数message是{默认消息},参数appendMessage是服务A给的{默认消息}+{扩展消息},最终会形成{默认消息}+{默认消息}+{扩展消息}
|
||||
//场景2:只有服务A=》
|
||||
// 服务A抛出异常{扩展消息}=》
|
||||
// 当前方法体拼接{默认消息}:{扩展消息} 并输出返回。
|
||||
//
|
||||
//总的来说,由于消息拼接是一个流式传递,由服务间传递,所以这里的消息可能存在A包含B,也可能出现B包含A,
|
||||
// 所以这里需要双重判定,A包含B=》返回A,B包含A=》返回B,否则返回拼接后的消息
|
||||
|
||||
if (message.contains(appendMessage)) {
|
||||
return message;
|
||||
}
|
||||
if (appendMessage.contains(message)) {
|
||||
return appendMessage;
|
||||
}
|
||||
//忽略默认错误信息,如果有其他错误消息体就不再返回默认的错误消息
|
||||
if (message.equals(ResultCode.ERROR.message())) {
|
||||
return appendMessage;
|
||||
}
|
||||
if (appendMessage.equals(ResultCode.ERROR.message())) {
|
||||
return message;
|
||||
}
|
||||
return CharSequenceUtil.format("{}-{}", message, appendMessage);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user