|
@@ -0,0 +1,61 @@
|
|
|
+package cn.com.ty.lift.system.framework.aspect;
|
|
|
+
|
|
|
+import cn.com.ty.lift.common.constants.ApiConstants;
|
|
|
+import cn.com.xwy.boot.web.dto.RestResponse;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
+import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
|
+import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
+import org.springframework.web.bind.annotation.ResponseStatus;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.util.LinkedHashMap;
|
|
|
+import java.util.LinkedList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author wcz
|
|
|
+ * @date 2019/12/10
|
|
|
+ * @description 全局异常捕获处理
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@RestController
|
|
|
+@ControllerAdvice
|
|
|
+public class GlobalDefaultExceptionHandler {
|
|
|
+
|
|
|
+ @ExceptionHandler(value = ExceptionInInitializerError.class)
|
|
|
+ @ResponseStatus(value = HttpStatus.BAD_REQUEST)
|
|
|
+ public Object badRequest() {
|
|
|
+ return RestResponse.exception("Bad Request");
|
|
|
+ }
|
|
|
+
|
|
|
+ @ExceptionHandler(value = Exception.class)
|
|
|
+ public Object defaultErrorHandler(final HttpServletRequest req, final HttpServletResponse resp, final Exception error) throws Exception {
|
|
|
+ // 原始错误集合数据偏多,包含native错误
|
|
|
+ final StackTraceElement[] stackTrace = error.getStackTrace();
|
|
|
+ // 过滤错误信息
|
|
|
+ final List<StackTraceElement> filters = new LinkedList<>();
|
|
|
+ for (StackTraceElement st : stackTrace)
|
|
|
+ if (st.getClassName().contains("cn.com.ty.lift.business") && st.getFileName().contains(".java")) filters.add(st);
|
|
|
+
|
|
|
+ final StackTraceElement[] dwStackTrace = new StackTraceElement[filters.size()];
|
|
|
+ // 计数器
|
|
|
+ Integer i = 0;
|
|
|
+ for (StackTraceElement filter : filters) {
|
|
|
+ dwStackTrace[i++] = filter;
|
|
|
+ }
|
|
|
+ // 填充StackTrace
|
|
|
+ error.setStackTrace(dwStackTrace);
|
|
|
+ LinkedHashMap<String, Object> errorMap = new LinkedHashMap<>();
|
|
|
+ errorMap.put("url", req.getRequestURI());
|
|
|
+ errorMap.put("status", resp.getStatus());
|
|
|
+ errorMap.put("message", error);
|
|
|
+ log.error(">>>>> GlobalDefaultException:{}, {}",errorMap,error);
|
|
|
+ if (error instanceof Exception) {
|
|
|
+ return RestResponse.fail(ApiConstants.RESULT_EXCEPTION, "操作异常,请重试");
|
|
|
+ }
|
|
|
+ return RestResponse.exception();
|
|
|
+ }
|
|
|
+}
|