|
|
@@ -4,6 +4,7 @@ import cn.hutool.core.util.StrUtil;
|
|
|
import com.ytpm.general.Result;
|
|
|
import com.ytpm.general.StatusCode;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.catalina.connector.ClientAbortException;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
import org.springframework.validation.ObjectError;
|
|
|
@@ -23,9 +24,11 @@ import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import javax.validation.ConstraintViolation;
|
|
|
import javax.validation.ConstraintViolationException;
|
|
|
+import java.io.IOException;
|
|
|
import java.text.MessageFormat;
|
|
|
import java.util.List;
|
|
|
import java.util.Set;
|
|
|
+import java.util.concurrent.TimeoutException;
|
|
|
|
|
|
/**
|
|
|
* @author Marx
|
|
|
@@ -38,12 +41,29 @@ public class CustomerExceptionHandler implements HandlerExceptionResolver {
|
|
|
@ExceptionHandler(Exception.class)
|
|
|
public Result handleException(Exception ex) {
|
|
|
//打印异常信息
|
|
|
- log.error("抛出异常信息,异常信息为:ex={}",ex);
|
|
|
- ex.printStackTrace();
|
|
|
+ log.error("系统异常: {}", ex.getMessage(), ex);
|
|
|
String msg = "查询超时,请稍后重试!";
|
|
|
+ // 根据异常类型返回不同的错误信息
|
|
|
+ if (ex instanceof NullPointerException) {
|
|
|
+ return new Result(StatusCode.ERROR, msg);
|
|
|
+ } else if (ex instanceof TimeoutException) {
|
|
|
+ return new Result(StatusCode.ERROR, msg);
|
|
|
+ }
|
|
|
return new Result(StatusCode.ERROR,msg);
|
|
|
}
|
|
|
|
|
|
+ @ResponseBody
|
|
|
+ @ExceptionHandler({ClientAbortException.class, IOException.class})
|
|
|
+ public Result handleNetworkException(Exception ex) {
|
|
|
+ if (ex instanceof ClientAbortException ||
|
|
|
+ (ex instanceof IOException && "Broken pipe".equals(ex.getMessage()))) {
|
|
|
+ // 客户端断开连接,不需要完整记录堆栈,减少日志量
|
|
|
+ log.warn("客户端断开连接: {}", ex.getMessage());
|
|
|
+ return new Result(StatusCode.ERROR, "网络开小差,请稍后重试!");
|
|
|
+ }
|
|
|
+ log.error("网络IO异常: {}", ex.getMessage());
|
|
|
+ return new Result(StatusCode.ERROR, "网络开小差,请稍后重试!");
|
|
|
+ }
|
|
|
/**
|
|
|
* 对象参数校验失败
|
|
|
* @param ex
|
|
|
@@ -66,7 +86,6 @@ public class CustomerExceptionHandler implements HandlerExceptionResolver {
|
|
|
@ExceptionHandler(ConstraintViolationException.class)
|
|
|
public Result handleConstraintViolationException(Exception ex) {
|
|
|
log.error("单个参数校验失败,异常信息为:ex={}",ex);
|
|
|
- ex.printStackTrace();
|
|
|
Set<ConstraintViolation<?>> sets = ((ConstraintViolationException) ex).getConstraintViolations();
|
|
|
if (!CollectionUtils.isEmpty(sets)) {
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
@@ -92,7 +111,6 @@ public class CustomerExceptionHandler implements HandlerExceptionResolver {
|
|
|
@ExceptionHandler(IllegalArgumentException.class)
|
|
|
public Result handleSpringCheckException(Exception ex) {
|
|
|
log.error("自定义校验异常信息捕获,异常信息为:ex={}",ex);
|
|
|
- ex.printStackTrace();
|
|
|
String msg = ex.getMessage();
|
|
|
return new Result(StatusCode.PARAMETER_CHECK_ERR, msg);
|
|
|
}
|
|
|
@@ -106,7 +124,6 @@ public class CustomerExceptionHandler implements HandlerExceptionResolver {
|
|
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
|
|
public Result handleMethodArgumentNotValidException(Exception ex) {
|
|
|
log.error(" 请求参数校验失败,异常信息为:ex={}",ex);
|
|
|
- ex.printStackTrace();
|
|
|
List<ObjectError> errors = ((MethodArgumentNotValidException) ex).getBindingResult().getAllErrors();
|
|
|
String msg = getValidExceptionMsg(errors);
|
|
|
return new Result(StatusCode.PARAMETER_CHECK_ERR, msg);
|
|
|
@@ -165,7 +182,6 @@ public class CustomerExceptionHandler implements HandlerExceptionResolver {
|
|
|
@ExceptionHandler(CustomerException.class)
|
|
|
public Result handleCustomerException(Exception ex){
|
|
|
log.error("自定义校验异常信息捕获,异常信息为:ex={}", ex);
|
|
|
- ex.printStackTrace();
|
|
|
String msg = ex.getMessage();
|
|
|
return new Result(StatusCode.ACCESS_ERR, msg);
|
|
|
}
|