|
@@ -1,15 +1,14 @@
|
|
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.alibaba.csp.sentinel.Tracer;
|
|
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.response.ResultData;
|
|
import com.fdkankan.common.response.ResultData;
|
|
|
|
+import java.util.List;
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
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;
|
|
@@ -19,10 +18,6 @@ import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
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.HttpServletResponse;
|
|
|
|
-import java.util.List;
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* 全局异常处理器
|
|
* 全局异常处理器
|
|
*/
|
|
*/
|
|
@@ -35,27 +30,33 @@ public class GlobalExceptionHandler {
|
|
*/
|
|
*/
|
|
@ResponseBody
|
|
@ResponseBody
|
|
@ExceptionHandler(value = Exception.class)
|
|
@ExceptionHandler(value = Exception.class)
|
|
- 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;
|
|
|
|
- }
|
|
|
|
|
|
+ public ResultData exceptionHandler(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Exception e) throws Exception {
|
|
|
|
+ log.error("服务错误:", e);
|
|
|
|
+ //增加sentinel异常次数
|
|
|
|
+ Tracer.trace(e);
|
|
|
|
+ return ResultData.error(ServerCode.SYSTEM_ERROR.code(), ExceptionUtil.stacktraceToString(e, 3000));
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 限流熔断异常处理
|
|
|
|
+ * 如果在这里捕获的话,就需要在接口中加入@SentinelResour注解
|
|
|
|
+ */
|
|
|
|
+// @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(), "系统繁忙,请稍后重试");
|
|
|
|
+// }
|
|
|
|
+
|
|
|
|
|
|
/**
|
|
/**
|
|
* 校验错误拦截处理
|
|
* 校验错误拦截处理
|