修改统一异常处理, 细化seata异常拦截

This commit is contained in:
ajaxfan
2021-04-16 15:13:39 +08:00
parent d6d3aabd7a
commit 7d9d87d95c
2 changed files with 17 additions and 11 deletions

View File

@ -35,6 +35,7 @@ import com.chinaunicom.mall.ebtp.common.util.JsonUtils;
import cn.hutool.core.convert.Convert; import cn.hutool.core.convert.Convert;
import cn.hutool.core.exceptions.ExceptionUtil; import cn.hutool.core.exceptions.ExceptionUtil;
import io.seata.core.context.RootContext; import io.seata.core.context.RootContext;
import io.seata.core.exception.RmTransactionException;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -51,7 +52,7 @@ import lombok.extern.slf4j.Slf4j;
@ControllerAdvice @ControllerAdvice
@ResponseBody @ResponseBody
@ConditionalOnProperty(name = "mconfig.exception-handle-enabled", matchIfMissing = true) @ConditionalOnProperty(name = "mconfig.exception-handle-enabled", matchIfMissing = true)
@Profile({"test", "local", "uat"}) @Profile({ "test", "local", "uat" })
public class BusinessExceptionHandlerAdviceDefault { public class BusinessExceptionHandlerAdviceDefault {
/** /**
@ -254,11 +255,10 @@ public class BusinessExceptionHandlerAdviceDefault {
body.put("errors", exception.getMessage()); body.put("errors", exception.getMessage());
body.put("error", HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase()); body.put("error", HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
// body.put("path", request.getRequestURI()); // body.put("path", request.getRequestURI());
return BaseResponse.fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), "系统异常", return BaseResponse.fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), "系统异常", Convert.toStr(body));
Convert.toStr(body));
} }
@ExceptionHandler({ TransactionSystemException.class }) @ExceptionHandler({ TransactionSystemException.class, RmTransactionException.class })
@ResponseStatus(HttpStatus.BAD_REQUEST) @ResponseStatus(HttpStatus.BAD_REQUEST)
public BaseResponse<String> handleTransactionSystemException(HttpServletRequest request, public BaseResponse<String> handleTransactionSystemException(HttpServletRequest request,
TransactionSystemException exception) { TransactionSystemException exception) {
@ -267,11 +267,12 @@ public class BusinessExceptionHandlerAdviceDefault {
String xid = RootContext.getXID(); String xid = RootContext.getXID();
if (StringUtils.isNotEmpty(xid)) { if (StringUtils.isNotEmpty(xid)) {
RootContext.unbind(); RootContext.unbind();
log.debug("TransactionSystemException ----- suspending current transaction,xid = {}", xid); log.info("TransactionSystemException ----- suspending current transaction,xid = {}", xid);
return BaseResponse.fail("系统繁忙,请重试", null);
} }
} }
return BaseResponse.fail("系统异常", exception.getMessage());
return BaseResponse.fail("系统繁忙,请重试", exception.getMessage());
} }
/** /**

View File

@ -35,6 +35,8 @@ import com.chinaunicom.mall.ebtp.common.util.JsonUtils;
import cn.hutool.core.convert.Convert; import cn.hutool.core.convert.Convert;
import cn.hutool.core.exceptions.ExceptionUtil; import cn.hutool.core.exceptions.ExceptionUtil;
import io.seata.core.context.RootContext; import io.seata.core.context.RootContext;
import io.seata.core.exception.RmTransactionException;
import io.seata.core.exception.TransactionException;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -76,7 +78,7 @@ public class BusinessExceptionHandlerAdvicePro {
* @param exception ServiceErrorException异常对象 * @param exception ServiceErrorException异常对象
* @return 响应 * @return 响应
*/ */
@ExceptionHandler(value = MethodArgumentNotValidException.class) @ExceptionHandler({ TransactionSystemException.class, RmTransactionException.class })
@ResponseStatus(HttpStatus.BAD_REQUEST) @ResponseStatus(HttpStatus.BAD_REQUEST)
public BaseResponse<String> handleInvalidMethodArgException(HttpServletRequest request, public BaseResponse<String> handleInvalidMethodArgException(HttpServletRequest request,
MethodArgumentNotValidException exception) { MethodArgumentNotValidException exception) {
@ -255,7 +257,7 @@ public class BusinessExceptionHandlerAdvicePro {
return BaseResponse.fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), "系统异常", Convert.toStr(body)); return BaseResponse.fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), "系统异常", Convert.toStr(body));
} }
@ExceptionHandler({ TransactionSystemException.class }) @ExceptionHandler({ TransactionException.class })
@ResponseStatus(HttpStatus.BAD_REQUEST) @ResponseStatus(HttpStatus.BAD_REQUEST)
public BaseResponse<String> handleTransactionSystemException(HttpServletRequest request, public BaseResponse<String> handleTransactionSystemException(HttpServletRequest request,
TransactionSystemException exception) { TransactionSystemException exception) {
@ -264,10 +266,13 @@ public class BusinessExceptionHandlerAdvicePro {
String xid = RootContext.getXID(); String xid = RootContext.getXID();
if (StringUtils.isNotEmpty(xid)) { if (StringUtils.isNotEmpty(xid)) {
RootContext.unbind(); RootContext.unbind();
log.debug("TransactionSystemException ----- suspending current transaction,xid = {}", xid); log.info("TransactionSystemException ----- suspending current transaction,xid = {}", xid);
return BaseResponse.fail("系统繁忙,请重试", null);
} }
} }
return BaseResponse.fail("系统繁忙,请重试", exception.getMessage());
return BaseResponse.fail("系统异常", exception.getMessage());
} }
/** /**