|
@@ -1,11 +1,15 @@
|
|
package com.fdkankan.common.exception;
|
|
package com.fdkankan.common.exception;
|
|
|
|
|
|
import cn.hutool.core.exceptions.ExceptionUtil;
|
|
import cn.hutool.core.exceptions.ExceptionUtil;
|
|
|
|
+import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
|
|
|
|
+import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
|
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.fdkankan.common.constant.ErrorCode;
|
|
import com.fdkankan.common.constant.ErrorCode;
|
|
import com.fdkankan.common.constant.ServerCode;
|
|
import com.fdkankan.common.constant.ServerCode;
|
|
import com.fdkankan.common.exception.BusinessException;
|
|
import com.fdkankan.common.exception.BusinessException;
|
|
import com.fdkankan.common.response.ResultData;
|
|
import com.fdkankan.common.response.ResultData;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.springframework.http.MediaType;
|
|
import org.springframework.validation.BindException;
|
|
import org.springframework.validation.BindException;
|
|
import org.springframework.validation.BindingResult;
|
|
import org.springframework.validation.BindingResult;
|
|
import org.springframework.validation.FieldError;
|
|
import org.springframework.validation.FieldError;
|
|
@@ -16,6 +20,7 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -30,23 +35,73 @@ public class GlobalExceptionHandler {
|
|
*/
|
|
*/
|
|
@ResponseBody
|
|
@ResponseBody
|
|
@ExceptionHandler(value = Exception.class)
|
|
@ExceptionHandler(value = Exception.class)
|
|
- public ResultData exceptionHandler(HttpServletRequest httpServletRequest, Exception e) {
|
|
|
|
- log.error("服务错误:", e);
|
|
|
|
- return ResultData.error(ServerCode.SYSTEM_ERROR.code(), ExceptionUtil.stacktraceToString(e, 3000));
|
|
|
|
|
|
+ public void exceptionHandler(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Exception e) throws Exception {
|
|
|
|
+ try{
|
|
|
|
+ log.error("服务错误:", e);
|
|
|
|
+// return ResultData.error(ServerCode.SYSTEM_ERROR.code(), ExceptionUtil.stacktraceToString(e, 3000));
|
|
|
|
+ // http状态码
|
|
|
|
+ httpServletResponse.setStatus(500);
|
|
|
|
+ httpServletResponse.setCharacterEncoding("utf-8");
|
|
|
|
+ httpServletResponse.setHeader("Content-Type", "application/json;charset=utf-8");
|
|
|
|
+ httpServletResponse.setContentType(MediaType.APPLICATION_JSON_VALUE);
|
|
|
|
+
|
|
|
|
+ new ObjectMapper()
|
|
|
|
+ .writeValue(
|
|
|
|
+ httpServletResponse.getWriter(),
|
|
|
|
+ ResultData.error(ServerCode.SYSTEM_ERROR.code(), ExceptionUtil.stacktraceToString(e, 3000))
|
|
|
|
+ );
|
|
|
|
+ }finally {
|
|
|
|
+ //最后抛出异常是为了sentinel熔断异常处理器能捕获到异常,进入熔断处理逻辑
|
|
|
|
+ throw e;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 限流熔断异常处理
|
|
|
|
+ */
|
|
|
|
+ @ResponseBody
|
|
|
|
+ @ExceptionHandler({FlowException.class, DegradeException.class})
|
|
|
|
+ public ResultData flowExceptionHandler(HttpServletRequest httpServletRequest, Exception e) {
|
|
|
|
+ String requestURI = httpServletRequest.getRequestURI();
|
|
|
|
+ String resource = null;
|
|
|
|
+ if(e instanceof FlowException){
|
|
|
|
+ resource = ((FlowException) e).getRule().getResource();
|
|
|
|
+ }
|
|
|
|
+ if(e instanceof DegradeException){
|
|
|
|
+ resource = ((DegradeException) e).getRule().getResource();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ log.error("请求限流:requestURI{},resource:{}", requestURI, resource);
|
|
|
|
+ return ResultData.error(ServerCode.SYSTEM_ERROR.code(), "系统繁忙,请稍后重试");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// /**
|
|
|
|
+// * 限流熔断异常处理
|
|
|
|
+// */
|
|
|
|
+// @ResponseBody
|
|
|
|
+// @ExceptionHandler(value = FlowException.class)
|
|
|
|
+// public ResultData flowExceptionHandler(HttpServletRequest httpServletRequest, FlowException e) {
|
|
|
|
+// String requestURI = httpServletRequest.getRequestURI();
|
|
|
|
+// String resource = e.getRule().getResource();;
|
|
|
|
+// log.error("请求限流:requestURI{},resource:{}", requestURI, resource);
|
|
|
|
+// return ResultData.error(ServerCode.SYSTEM_ERROR.code(), "系统繁忙,请稍后重试");
|
|
|
|
+// }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 校验错误拦截处理
|
|
* 校验错误拦截处理
|
|
*/
|
|
*/
|
|
@ResponseBody
|
|
@ResponseBody
|
|
@ExceptionHandler({BindException.class,MethodArgumentNotValidException.class})
|
|
@ExceptionHandler({BindException.class,MethodArgumentNotValidException.class})
|
|
- public ResultData validationBodyException(Exception exception) {
|
|
|
|
|
|
+ public ResultData validationBodyException(Exception e) {
|
|
BindingResult result = null;
|
|
BindingResult result = null;
|
|
- if(exception instanceof MethodArgumentNotValidException){
|
|
|
|
- result = ((MethodArgumentNotValidException) exception).getBindingResult();
|
|
|
|
|
|
+ if(e instanceof MethodArgumentNotValidException){
|
|
|
|
+ result = ((MethodArgumentNotValidException) e).getBindingResult();
|
|
}
|
|
}
|
|
- if(exception instanceof BindException){
|
|
|
|
- result = ((BindException) exception).getBindingResult();
|
|
|
|
|
|
+ if(e instanceof BindException){
|
|
|
|
+ result = ((BindException) e).getBindingResult();
|
|
}
|
|
}
|
|
String message = "";
|
|
String message = "";
|
|
if (result.hasErrors()) {
|
|
if (result.hasErrors()) {
|