无忧回调修改

This commit is contained in:
GYJ
2024-12-04 18:08:15 +08:00
parent e8a2e9bee1
commit 32a8eb78d6
5 changed files with 38 additions and 28 deletions

View File

@@ -7,6 +7,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.NoHandlerFoundException;
@@ -33,33 +34,37 @@ public class SqxExceptionHandler {
@ExceptionHandler(NoHandlerFoundException.class)
public Result handlerNoFoundException(Exception e, WebRequest webRequest) {
String requestUrl = webRequest.getDescription(false).split(" ")[1];
logger.error("请求路径不存在:{}", requestUrl);
logErrorInfo(webRequest);
logger.error(e.getMessage(), e);
return Result.error(404, "路径不存在,请检查路径是否正确");
}
@ExceptionHandler(DuplicateKeyException.class)
public Result handleDuplicateKeyException(DuplicateKeyException e, WebRequest webRequest) {
String requestUrl = webRequest.getDescription(false).split(" ")[1];
logger.error("请求路径:{},数据库中已存在该记录", requestUrl);
logErrorInfo(webRequest);
logger.error(e.getMessage(), e);
return Result.error("数据库中已存在该记录");
}
@ExceptionHandler(AuthorizationException.class)
public Result handleAuthorizationException(AuthorizationException e, WebRequest webRequest) {
String requestUrl = webRequest.getDescription(false).split(" ")[1];
logger.error("请求路径:{},没有权限", requestUrl);
logErrorInfo(webRequest);
logger.error(e.getMessage(), e);
return Result.error("没有权限,请联系管理员授权");
}
@ExceptionHandler(Exception.class)
public Result handleException(Exception e, WebRequest webRequest) {
String requestUrl = webRequest.getDescription(false).split(" ")[1];
logger.error("请求路径:{},发生异常", requestUrl);
logErrorInfo(webRequest);
logger.error(e.getMessage(), e);
return Result.error();
}
private void logErrorInfo(WebRequest webRequest) {
if (webRequest instanceof ServletWebRequest) {
ServletWebRequest servletWebRequest = (ServletWebRequest) webRequest;
String requestUrl = servletWebRequest.getRequest().getRequestURI();
logger.error("请求路径:{},发生异常", requestUrl);
}
}
}