|
@@ -4,6 +4,7 @@ package com.fs.app.exception;
|
|
|
|
|
|
|
|
import com.fs.common.core.domain.R;
|
|
import com.fs.common.core.domain.R;
|
|
|
import com.fs.common.exception.CustomException;
|
|
import com.fs.common.exception.CustomException;
|
|
|
|
|
+import org.apache.catalina.connector.ClientAbortException;
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.dao.DuplicateKeyException;
|
|
import org.springframework.dao.DuplicateKeyException;
|
|
@@ -12,10 +13,12 @@ import org.springframework.validation.BindException;
|
|
|
import org.springframework.validation.FieldError;
|
|
import org.springframework.validation.FieldError;
|
|
|
import org.springframework.validation.ObjectError;
|
|
import org.springframework.validation.ObjectError;
|
|
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
|
|
|
+import org.springframework.web.bind.MissingServletRequestParameterException;
|
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
import org.springframework.web.servlet.NoHandlerFoundException;
|
|
import org.springframework.web.servlet.NoHandlerFoundException;
|
|
|
|
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
|
|
@@ -50,27 +53,68 @@ public class FSExceptionHandler {
|
|
|
return R.error("数据库中已存在该记录");
|
|
return R.error("数据库中已存在该记录");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 客户端主动断开(切后台/弱网),不按系统错误打 ERROR
|
|
|
|
|
+ */
|
|
|
|
|
+ @ExceptionHandler(ClientAbortException.class)
|
|
|
|
|
+ public void handleClientAbortException(ClientAbortException e) {
|
|
|
|
|
+ logger.warn("客户端中断连接: {}", e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @ExceptionHandler(MissingServletRequestParameterException.class)
|
|
|
|
|
+ public R handleMissingServletRequestParameterException(MissingServletRequestParameterException e) {
|
|
|
|
|
+ logger.warn("缺少请求参数: {}", e.getMessage());
|
|
|
|
|
+ return R.error("缺少必要参数: " + e.getParameterName());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @ExceptionHandler(NumberFormatException.class)
|
|
|
|
|
+ public R handleNumberFormatException(NumberFormatException e) {
|
|
|
|
|
+ logger.warn("参数格式错误: {}", e.getMessage());
|
|
|
|
|
+ return R.error("参数格式错误,请检查登录状态或入参");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @ExceptionHandler(IllegalArgumentException.class)
|
|
|
|
|
+ public R handleIllegalArgumentException(IllegalArgumentException e) {
|
|
|
|
|
+ logger.warn("非法参数: {}", e.getMessage());
|
|
|
|
|
+ return R.error(e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
@ExceptionHandler(Exception.class)
|
|
@ExceptionHandler(Exception.class)
|
|
|
public R handleException(Exception e){
|
|
public R handleException(Exception e){
|
|
|
|
|
+ if (isClientAbort(e)) {
|
|
|
|
|
+ logger.warn("客户端中断连接: {}", e.getMessage());
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
logger.error(e.getMessage(), e);
|
|
logger.error(e.getMessage(), e);
|
|
|
if (e instanceof BindException){
|
|
if (e instanceof BindException){
|
|
|
BindException ex = (BindException)e;
|
|
BindException ex = (BindException)e;
|
|
|
- List<ObjectError> allErrors = ex.getAllErrors();//捕获的所有错误对象
|
|
|
|
|
|
|
+ List<ObjectError> allErrors = ex.getAllErrors();
|
|
|
ObjectError error = allErrors.get(0);
|
|
ObjectError error = allErrors.get(0);
|
|
|
- String defaultMessage = error.getDefaultMessage();//异常内容
|
|
|
|
|
|
|
+ String defaultMessage = error.getDefaultMessage();
|
|
|
|
|
|
|
|
return R.error(defaultMessage);
|
|
return R.error(defaultMessage);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/* if(e instanceof IllegalArgumentException){
|
|
|
|
|
- return R.error(e.getMessage());
|
|
|
|
|
- } else {
|
|
|
|
|
- return R.error();
|
|
|
|
|
- }*/
|
|
|
|
|
-
|
|
|
|
|
return R.error(e.getMessage());
|
|
return R.error(e.getMessage());
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ private boolean isClientAbort(Throwable e) {
|
|
|
|
|
+ Throwable cur = e;
|
|
|
|
|
+ while (cur != null) {
|
|
|
|
|
+ if (cur instanceof ClientAbortException) {
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (cur instanceof IOException) {
|
|
|
|
|
+ String msg = cur.getMessage();
|
|
|
|
|
+ if (msg != null && (msg.contains("远程主机强迫关闭") || msg.contains("Broken pipe")
|
|
|
|
|
+ || msg.contains("Connection reset") || msg.contains("你的主机中的软件中止了一个已建立的连接"))) {
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ cur = cur.getCause();
|
|
|
|
|
+ }
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
@ExceptionHandler(BindException.class)
|
|
@ExceptionHandler(BindException.class)
|
|
|
public R bindExceptionHandler(BindException e) {
|
|
public R bindExceptionHandler(BindException e) {
|
|
|
FieldError error = e.getFieldError();
|
|
FieldError error = e.getFieldError();
|