带着更好格式的报错通知我
This commit is contained in:
parent
4ae9cfd5ba
commit
0bff98f28c
@ -11,9 +11,15 @@ import org.springframework.http.HttpStatus;
|
|||||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||||
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.context.request.RequestContextHolder;
|
||||||
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Enumeration;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @date 2022年09月23日 10:19
|
* @date 2022年09月23日 10:19
|
||||||
@ -63,12 +69,53 @@ public class CustomExceptionHandle {
|
|||||||
public ApiResponse<String> handle(Exception e) {
|
public ApiResponse<String> handle(Exception e) {
|
||||||
LOGGER.error("系统异常 -> {}", e.getMessage(), e);
|
LOGGER.error("系统异常 -> {}", e.getMessage(), e);
|
||||||
NotifyFactory.to().sendTo(
|
NotifyFactory.to().sendTo(
|
||||||
new NotifyContent("帧途后台报错了!", e.getMessage() + "\n\n" + Arrays.toString(e.getStackTrace())),
|
new NotifyContent(
|
||||||
|
"帧途后台报错了!",
|
||||||
|
e.getMessage() + "\n---\n请求主体:\n```\n" + getRequestAsText() + "\n```\n---\n错误栈:\n```\n" + getStackTrace(e) + "\n```"
|
||||||
|
),
|
||||||
"default_user"
|
"default_user"
|
||||||
);
|
);
|
||||||
return ApiResponse.buildResult(BizCodeEnum.SERVER_UNKONWN_ERROR);
|
return ApiResponse.buildResult(BizCodeEnum.SERVER_UNKONWN_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getStackTrace(Throwable e) {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
StackTraceElement[] stackTrace = e.getStackTrace();
|
||||||
|
sb.append(e.getClass().getName()).append(": ").append(e.getMessage()).append("\r\n");
|
||||||
|
for (StackTraceElement stackTraceElement : stackTrace) {
|
||||||
|
sb.append("\tat ").append(stackTraceElement.toString()).append("\r\n");
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRequestAsText() {
|
||||||
|
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||||
|
if (attributes == null) {
|
||||||
|
return "---";
|
||||||
|
}
|
||||||
|
HttpServletRequest request = attributes.getRequest();
|
||||||
|
StringBuilder rawReq = new StringBuilder();
|
||||||
|
rawReq.append(request.getMethod()).append(" ").append(request.getRequestURL());
|
||||||
|
String queryString = request.getQueryString();
|
||||||
|
if (queryString != null) {
|
||||||
|
rawReq.append("?").append(queryString);
|
||||||
|
}
|
||||||
|
rawReq.append("\r\n");
|
||||||
|
Enumeration<String> headerNames = request.getHeaderNames();
|
||||||
|
while (headerNames.hasMoreElements()) {
|
||||||
|
String headerName = headerNames.nextElement();
|
||||||
|
rawReq.append(headerName).append(": ").append(request.getHeader(headerName)).append("\r\n");
|
||||||
|
}
|
||||||
|
rawReq.append("\r\n");
|
||||||
|
// 获取body
|
||||||
|
try {
|
||||||
|
rawReq.append(request.getReader().lines().collect(Collectors.joining("\r\n")));
|
||||||
|
rawReq.append("\r\n");
|
||||||
|
} catch (IOException ignored) {
|
||||||
|
}
|
||||||
|
return rawReq.toString();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 移动端自定义异常统一处理类
|
* 移动端自定义异常统一处理类
|
||||||
*/
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user