package com.tqxd.user.service.common;
import com.tqxd.user.common.ResponsePacket;
import com.tqxd.user.common.exception.BizExceptionEnum;
import com.tqxd.user.common.exception.BusinessException;
import com.tqxd.user.service.encry.annotation.Encrypt;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.HashMap;
/**
* 统一异常处理
*/
@Slf4j
@RestControllerAdvice
@Encrypt
public class GlobalExceptionHandler {
private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
/**
* 业务异常处理
*/
@SuppressWarnings("rawtypes")
@ExceptionHandler(BusinessException.class)
@ResponseBody
public ResponsePacket handleBusinessException(BusinessException ex) {
logger.error("业务错误信息: {}", ex.getMsg());
for (int i = 0; i < ex.getStackTrace().length; i++) {
StackTraceElement stackTraceElement = ex.getStackTrace()[i];
if (stackTraceElement.getClassName().startsWith("com.tqxd.") && !stackTraceElement
.getClassName().contains("$$")) {
logger.error("{}", stackTraceElement);
}
}
if (ex.getData() == null) {
return ResponsePacket.onError(ex.getCode(), ex.getMsg(), null);
}
return ResponsePacket.onError(ex.getCode(), ex.getMsg(), ex.getData());
}
/**
* 非法参数异常处理 参数验证返回提示信息@Valid
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseBody
public ResponsePacket paramError(MethodArgumentNotValidException e) {
BindingResult bindingResult = e.getBindingResult();
BizExceptionEnum bizExceptionEnum = BizExceptionEnum.PARAM_VALID_FAIL;
return ResponsePacket.onError(HttpStatus.BAD_REQUEST.value(), String.format(
bizExceptionEnum.getMsg(), bindingResult.getFieldError().getField(),bindingResult.getFieldError().getDefaultMessage()), new HashMap<>(0));
}
/**
* 非法参数异常处理
*/
@ExceptionHandler(IllegalArgumentException.class)
@ResponseBody
public ResponsePacket paramError(IllegalArgumentException e) {
String bindingResult = e.getMessage();
BizExceptionEnum bizExceptionEnum = BizExceptionEnum.PARAM_VALID_FAIL;
return ResponsePacket.onError(HttpStatus.BAD_REQUEST.value(), String.format(
bizExceptionEnum.getMsg(), bindingResult), new HashMap<>(0));
}
/**
* 非法参数异常处理
*/
@ExceptionHandler(BindException.class)
@ResponseBody
public ResponsePacket paramError(BindException e) {
BindingResult bindingResult = e.getBindingResult();
BizExceptionEnum bizExceptionEnum = BizExceptionEnum.PARAM_VALID_FAIL;
return ResponsePacket.onError(HttpStatus.BAD_REQUEST.value(), String.format(
bizExceptionEnum.getMsg(), bindingResult.getFieldError().getField(),bindingResult.getFieldError().getDefaultMessage()), new HashMap<>(0));
}
/**
* 系统内部异常处理
*
* @param ex
* @return
*/
@SuppressWarnings("rawtypes")
@ExceptionHandler(Exception.class)
@ResponseBody
public ResponsePacket handleAllException(Exception ex) {
logger.error(ex.getMessage(), ex);
return ResponsePacket.onError();
}
}