|
@@ -1,25 +1,26 @@
|
|
|
package com.simuwang.deploy.components;
|
|
|
|
|
|
+import cn.hutool.core.exceptions.ExceptionUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
+import com.simuwang.base.common.exception.APIException;
|
|
|
import com.simuwang.base.common.exception.ErrorInfo;
|
|
|
import jakarta.servlet.ServletException;
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.apache.shiro.authz.UnauthenticatedException;
|
|
|
+import org.apache.shiro.authz.UnauthorizedException;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
-import org.springframework.boot.autoconfigure.web.ErrorProperties;
|
|
|
-import org.springframework.boot.autoconfigure.web.ServerProperties;
|
|
|
import org.springframework.core.Ordered;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.lang.Nullable;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.web.servlet.HandlerExceptionResolver;
|
|
|
import org.springframework.web.servlet.ModelAndView;
|
|
|
+import org.springframework.web.servlet.resource.NoResourceFoundException;
|
|
|
import org.springframework.web.util.WebUtils;
|
|
|
|
|
|
-import java.io.PrintWriter;
|
|
|
-import java.io.StringWriter;
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
|
/**
|
|
@@ -34,17 +35,17 @@ public class ErrorInfoBuilder implements HandlerExceptionResolver, Ordered {
|
|
|
*/
|
|
|
private final static String ERROR_NAME = "simuwang.error";
|
|
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
|
|
- /**
|
|
|
- * 错误配置(ErrorConfiguration)
|
|
|
- */
|
|
|
- private final ErrorProperties errorProperties;
|
|
|
-
|
|
|
- /**
|
|
|
- * 错误构造器 (Constructor) 传递配置属性:server.xx -> server.error.xx
|
|
|
- */
|
|
|
- public ErrorInfoBuilder(ServerProperties serverProperties) {
|
|
|
- this.errorProperties = serverProperties.getError();
|
|
|
- }
|
|
|
+// /**
|
|
|
+// * 错误配置(ErrorConfiguration)
|
|
|
+// */
|
|
|
+// private final ErrorProperties errorProperties;
|
|
|
+//
|
|
|
+// /**
|
|
|
+// * 错误构造器 (Constructor) 传递配置属性:server.xx -> server.error.xx
|
|
|
+// */
|
|
|
+// public ErrorInfoBuilder(ServerProperties serverProperties) {
|
|
|
+// this.errorProperties = serverProperties.getError();
|
|
|
+// }
|
|
|
|
|
|
/**
|
|
|
* 构建错误信息.(ErrorInfo)
|
|
@@ -61,11 +62,23 @@ public class ErrorInfoBuilder implements HandlerExceptionResolver, Ordered {
|
|
|
ErrorInfo errorInfo = new ErrorInfo();
|
|
|
errorInfo.setTime(LocalDateTime.now().toString());
|
|
|
errorInfo.setUrl(url);
|
|
|
- errorInfo.setError(error.getMessage());
|
|
|
+ String msg;
|
|
|
+ if (error instanceof NoResourceFoundException) {
|
|
|
+ msg = "请求资源找不到";
|
|
|
+ } else if (error instanceof UnauthorizedException) {
|
|
|
+ msg = "没有对应接口的权限";
|
|
|
+ } else if (error instanceof APIException e) {
|
|
|
+ msg = e.getMsg();
|
|
|
+ } else if (error instanceof UnauthenticatedException e) {
|
|
|
+ msg = e.getMessage();
|
|
|
+ } else {
|
|
|
+ msg = error.getMessage();
|
|
|
+ }
|
|
|
+ errorInfo.setError(msg);
|
|
|
errorInfo.setStatusCode(getHttpStatus(request).value());
|
|
|
errorInfo.setReasonPhrase(getHttpStatus(request).getReasonPhrase());
|
|
|
- errorInfo.setStackTrace(getStackTraceInfo(error, isIncludeStackTrace(request)));
|
|
|
- logger.error(StrUtil.format("{} 接口请求错误:{}", url, errorInfo.getError()));
|
|
|
+ errorInfo.setStackTrace(ExceptionUtil.stacktraceToString(error));
|
|
|
+ logger.error(StrUtil.format("{} 接口请求错误:{}", url, errorInfo.getStackTrace()));
|
|
|
return errorInfo;
|
|
|
}
|
|
|
|
|
@@ -112,43 +125,43 @@ public class ErrorInfoBuilder implements HandlerExceptionResolver, Ordered {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 获取堆栈轨迹(StackTrace)
|
|
|
- *
|
|
|
- * @see org.springframework.boot.web.servlet.error.DefaultErrorAttributes #addStackTrace
|
|
|
- */
|
|
|
- public String getStackTraceInfo(Throwable error, boolean flag) {
|
|
|
- if (!flag) {
|
|
|
- return "omitted";
|
|
|
- }
|
|
|
- StringWriter stackTrace = new StringWriter();
|
|
|
- error.printStackTrace(new PrintWriter(stackTrace));
|
|
|
- stackTrace.flush();
|
|
|
- return stackTrace.toString();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 判断是否包含堆栈轨迹.(isIncludeStackTrace)
|
|
|
- *
|
|
|
- * @see org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController #isIncludeStackTrace
|
|
|
- */
|
|
|
- public boolean isIncludeStackTrace(HttpServletRequest request) {
|
|
|
-
|
|
|
- //读取错误配置(server.error.include-stacktrace=NEVER)
|
|
|
- ErrorProperties.IncludeAttribute includeStacktrace = errorProperties.getIncludeStacktrace();
|
|
|
+// /**
|
|
|
+// * 获取堆栈轨迹(StackTrace)
|
|
|
+// *
|
|
|
+// * @see org.springframework.boot.web.servlet.error.DefaultErrorAttributes #addStackTrace
|
|
|
+// */
|
|
|
+// public String getStackTraceInfo(Throwable error, boolean flag) {
|
|
|
+// if (!flag) {
|
|
|
+// return "omitted";
|
|
|
+// }
|
|
|
+// StringWriter stackTrace = new StringWriter();
|
|
|
+// error.printStackTrace(new PrintWriter(stackTrace));
|
|
|
+// stackTrace.flush();
|
|
|
+// return stackTrace.toString();
|
|
|
+// }
|
|
|
|
|
|
- //情况1:若IncludeStacktrace为ALWAYS
|
|
|
- if (includeStacktrace == ErrorProperties.IncludeAttribute.ALWAYS) {
|
|
|
- return true;
|
|
|
- }
|
|
|
- //情况2:若请求参数含有trace
|
|
|
- if (includeStacktrace == ErrorProperties.IncludeAttribute.ON_PARAM) {
|
|
|
- String parameter = request.getParameter("trace");
|
|
|
- return parameter != null && !"false".equalsIgnoreCase(parameter);
|
|
|
- }
|
|
|
- //情况3:其它情况
|
|
|
- return false;
|
|
|
- }
|
|
|
+// /**
|
|
|
+// * 判断是否包含堆栈轨迹.(isIncludeStackTrace)
|
|
|
+// *
|
|
|
+// * @see org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController #isIncludeStackTrace
|
|
|
+// */
|
|
|
+// public boolean isIncludeStackTrace(HttpServletRequest request) {
|
|
|
+//
|
|
|
+// //读取错误配置(server.error.include-stacktrace=NEVER)
|
|
|
+// ErrorProperties.IncludeAttribute includeStacktrace = errorProperties.getIncludeStacktrace();
|
|
|
+//
|
|
|
+// //情况1:若IncludeStacktrace为ALWAYS
|
|
|
+// if (includeStacktrace == ErrorProperties.IncludeAttribute.ALWAYS) {
|
|
|
+// return true;
|
|
|
+// }
|
|
|
+// //情况2:若请求参数含有trace
|
|
|
+// if (includeStacktrace == ErrorProperties.IncludeAttribute.ON_PARAM) {
|
|
|
+// String parameter = request.getParameter("trace");
|
|
|
+// return parameter != null && !"false".equalsIgnoreCase(parameter);
|
|
|
+// }
|
|
|
+// //情况3:其它情况
|
|
|
+// return false;
|
|
|
+// }
|
|
|
|
|
|
/**
|
|
|
* 保存错误/异常.
|