增加seata异常拦截

This commit is contained in:
ajaxfan
2021-03-24 18:33:04 +08:00
parent 15de420589
commit dbfc63d7c8

View File

@ -34,13 +34,13 @@ import com.chinaunicom.mall.ebtp.common.util.JsonUtils;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.exceptions.ExceptionUtil;
import io.seata.core.context.RootContext;
import io.seata.core.exception.RmTransactionException;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
/**
* 异常处理
* 拦截BindException异常返回HttpStatus是400的绑定错误信息
* 异常处理 拦截BindException异常返回HttpStatus是400的绑定错误信息
* 拦截FrameException异常返回HttpStatus是406的业务处理错误信息(支持自定义状态码)
* 拦截Exception异常返回HttpStatus是500服务器内部异常
*
@ -53,7 +53,6 @@ import lombok.extern.slf4j.Slf4j;
@ConditionalOnProperty(name = "mconfig.exception-handle-enabled", matchIfMissing = true)
public class BusinessExceptionHandlerAdvice {
/**
* 业务异常处理
*
@ -80,7 +79,8 @@ public class BusinessExceptionHandlerAdvice {
*/
@ExceptionHandler(value = MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public BaseResponse<String> handleInvalidMethodArgException(HttpServletRequest request, MethodArgumentNotValidException exception) {
public BaseResponse<String> handleInvalidMethodArgException(HttpServletRequest request,
MethodArgumentNotValidException exception) {
// 堆栈信息转为字符串
log.info(ExceptionUtil.stacktraceToString(exception));
// 按需重新封装需要返回的错误信息
@ -110,8 +110,7 @@ public class BusinessExceptionHandlerAdvice {
*/
@ExceptionHandler(value = BindException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public BaseResponse<String> methodArgumentNotValidHandler(
HttpServletRequest request, BindException exception) {
public BaseResponse<String> methodArgumentNotValidHandler(HttpServletRequest request, BindException exception) {
// 按需重新封装需要返回的错误信息
List<ArgumentInvalidResult> invalidArguments = new ArrayList<>();
// 解析原错误信息,封装后返回,此处返回非法的字段名称,原始值,错误信息
@ -138,7 +137,8 @@ public class BusinessExceptionHandlerAdvice {
*/
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
public BaseResponse<String> handleMethodNotSupportedException(HttpServletRequest request, HttpRequestMethodNotSupportedException exception) {
public BaseResponse<String> handleMethodNotSupportedException(HttpServletRequest request,
HttpRequestMethodNotSupportedException exception) {
Map<String, Object> body = new HashMap<>();
body.put("errors", exception.getMessage());
body.put("error", HttpStatus.METHOD_NOT_ALLOWED.getReasonPhrase());
@ -155,7 +155,8 @@ public class BusinessExceptionHandlerAdvice {
*/
@ExceptionHandler(MissingServletRequestParameterException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public BaseResponse<String> handleMissingParameterException(HttpServletRequest request, MissingServletRequestParameterException exception) {
public BaseResponse<String> handleMissingParameterException(HttpServletRequest request,
MissingServletRequestParameterException exception) {
Map<String, Object> body = new HashMap<>();
body.put("errors", exception.getMessage());
body.put("error", HttpStatus.BAD_REQUEST.getReasonPhrase());
@ -192,7 +193,8 @@ public class BusinessExceptionHandlerAdvice {
*/
@ExceptionHandler(EmptyResultDataAccessException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public BaseResponse<String> handleDataEmptyException(HttpServletRequest request, EmptyResultDataAccessException exception) {
public BaseResponse<String> handleDataEmptyException(HttpServletRequest request,
EmptyResultDataAccessException exception) {
Map<String, Object> body = new HashMap<>();
body.put("errors", exception.getMessage());
body.put("error", HttpStatus.BAD_REQUEST.getReasonPhrase());
@ -226,7 +228,8 @@ public class BusinessExceptionHandlerAdvice {
*/
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public BaseResponse<String> handleMethodArgumentTypeException(HttpServletRequest request, MethodArgumentTypeMismatchException exception) {
public BaseResponse<String> handleMethodArgumentTypeException(HttpServletRequest request,
MethodArgumentTypeMismatchException exception) {
Map<String, Object> body = new HashMap<>();
body.put("errors", exception.getMessage());
body.put("error", HttpStatus.BAD_REQUEST.getReasonPhrase());
@ -250,12 +253,14 @@ public class BusinessExceptionHandlerAdvice {
body.put("errors", exception.getMessage());
body.put("error", HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
// body.put("path", request.getRequestURI());
return BaseResponse.fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), "系统异常:" + exception.getMessage(), Convert.toStr(body));
return BaseResponse.fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), "系统异常:" + exception.getMessage(),
Convert.toStr(body));
}
@ExceptionHandler({TransactionSystemException.class})
@ExceptionHandler({ TransactionSystemException.class, RmTransactionException.class })
@ResponseStatus(HttpStatus.BAD_REQUEST)
public BaseResponse<String> handleTransactionSystemException(HttpServletRequest request, TransactionSystemException exception) {
public BaseResponse<String> handleTransactionSystemException(HttpServletRequest request,
TransactionSystemException exception) {
log.info(ExceptionUtil.stacktraceToString(exception));
if (((String) Objects.requireNonNull(exception.getMessage())).contains("may be has finished")) {
String xid = RootContext.getXID();