mirror of
https://github.com/continew-org/continew-admin.git
synced 2025-09-10 07:02:47 +08:00
feat: 新增NoHandlerFoundException、HttpRequestMethodNotSupportedException异常处理 @dom-w
This commit is contained in:
@@ -17,15 +17,16 @@
|
||||
package top.continew.admin.common.config.exception;
|
||||
|
||||
import cn.hutool.core.text.CharSequenceUtil;
|
||||
import cn.hutool.core.util.NumberUtil;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
|
||||
import org.springframework.web.multipart.MultipartException;
|
||||
import org.springframework.web.servlet.NoHandlerFoundException;
|
||||
import top.continew.starter.core.exception.BadRequestException;
|
||||
import top.continew.starter.core.exception.BusinessException;
|
||||
import top.continew.starter.web.model.R;
|
||||
@@ -34,6 +35,7 @@ import top.continew.starter.web.model.R;
|
||||
* 全局异常处理器
|
||||
*
|
||||
* @author Charles7c
|
||||
* @author echo
|
||||
* @since 2024/8/7 20:21
|
||||
*/
|
||||
@Slf4j
|
||||
@@ -73,7 +75,7 @@ public class GlobalExceptionHandler {
|
||||
* 拦截文件上传异常-超过上传大小限制
|
||||
*/
|
||||
@ExceptionHandler(MultipartException.class)
|
||||
public R handleRequestTooBigException(MultipartException e, HttpServletRequest request) {
|
||||
public R handleMultipartException(MultipartException e, HttpServletRequest request) {
|
||||
log.error("[{}] {}", request.getMethod(), request.getRequestURI(), e);
|
||||
String msg = e.getMessage();
|
||||
R defaultFail = R.fail(String.valueOf(HttpStatus.BAD_REQUEST.value()), msg);
|
||||
@@ -85,14 +87,33 @@ public class GlobalExceptionHandler {
|
||||
if (null != cause) {
|
||||
msg = msg.concat(cause.getMessage().toLowerCase());
|
||||
}
|
||||
if (msg.contains("size") && msg.contains("exceed")) {
|
||||
sizeLimit = CharSequenceUtil.subBetween(msg, "the maximum size ", " for");
|
||||
} else if (msg.contains("larger than")) {
|
||||
if (msg.contains("larger than")) {
|
||||
sizeLimit = CharSequenceUtil.subAfter(msg, "larger than ", true);
|
||||
} else if (msg.contains("size") && msg.contains("exceed")) {
|
||||
sizeLimit = CharSequenceUtil.subBetween(msg, "the maximum size ", " for");
|
||||
} else {
|
||||
return defaultFail;
|
||||
}
|
||||
String errorMsg = "请上传小于 %sKB 的文件".formatted(NumberUtil.parseLong(sizeLimit) / 1024);
|
||||
return R.fail(String.valueOf(HttpStatus.BAD_REQUEST.value()), errorMsg);
|
||||
return R.fail(String.valueOf(HttpStatus.BAD_REQUEST.value()), "请上传小于 %s bytes 的文件".formatted(sizeLimit));
|
||||
}
|
||||
|
||||
/**
|
||||
* 拦截请求 URL 不存在异常
|
||||
*/
|
||||
@ExceptionHandler(NoHandlerFoundException.class)
|
||||
public R handleNoHandlerFoundException(NoHandlerFoundException e, HttpServletRequest request) {
|
||||
log.error("[{}] {}", request.getMethod(), request.getRequestURI(), e);
|
||||
return R.fail(String.valueOf(HttpStatus.NOT_FOUND.value()), "请求 URL '%s' 不存在".formatted(request
|
||||
.getRequestURI()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 拦截不支持的 HTTP 请求方法异常
|
||||
*/
|
||||
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
|
||||
public R handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e,
|
||||
HttpServletRequest request) {
|
||||
log.error("[{}] {}", request.getMethod(), request.getRequestURI(), e);
|
||||
return R.fail(String.valueOf(HttpStatus.METHOD_NOT_ALLOWED.value()), "请求方式 '%s' 不支持".formatted(e.getMethod()));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user