From d696a6c20589262d6a550a702822c7cbc9a47d30 Mon Sep 17 00:00:00 2001 From: mzr <1562242162@qq.com> Date: Wed, 6 Nov 2024 09:49:35 +0800 Subject: [PATCH] =?UTF-8?q?feat(exception):=20=E5=AE=8C=E5=96=84=E5=85=A8?= =?UTF-8?q?=E5=B1=80=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86=E5=99=A8=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加对 HttpRequestMethodNotSupportedException、MissingServletRequestParameterException 和 TypeMismatchException 的处理 -增加未知运行时异常的处理 - 优化异常处理逻辑,统一错误信息格式 - 引入 HttpServletRequest 以获取请求路径信息 --- .../exception/GlobalExceptionHandler.java | 53 ++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/crm-common/src/main/java/com/yb/lb/common/exception/GlobalExceptionHandler.java b/crm-common/src/main/java/com/yb/lb/common/exception/GlobalExceptionHandler.java index 25118cf..2abeadd 100644 --- a/crm-common/src/main/java/com/yb/lb/common/exception/GlobalExceptionHandler.java +++ b/crm-common/src/main/java/com/yb/lb/common/exception/GlobalExceptionHandler.java @@ -5,13 +5,18 @@ import com.yb.lb.common.utils.ErrorUtils; import com.yb.lb.common.utils.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.TypeMismatchException; import org.springframework.stereotype.Component; import org.springframework.validation.BindException; +import org.springframework.web.HttpRequestMethodNotSupportedException; import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.MissingServletRequestParameterException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; +import javax.servlet.http.HttpServletRequest; + /** * 全局异常处理器 * @@ -72,7 +77,53 @@ public class GlobalExceptionHandler { public Object validExceptionHandler(MethodArgumentNotValidException e) { log.error(e.getMessage(), e); String message = e.getBindingResult().getFieldError().getDefaultMessage(); - return AjaxResult.error(message); + return AjaxResult.error(ErrorUtils.ERROR, message); } + /** + * 请求方式不支持 + */ + @ResponseBody + @ExceptionHandler(HttpRequestMethodNotSupportedException.class) + public AjaxResult handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e, + HttpServletRequest request) { + String requestURI = request.getRequestURI(); + log.error("请求地址'" + requestURI + "',不支持'" + e.getMethod() + "'请求"); + return AjaxResult.error(ErrorUtils.ERROR, e.getMessage()); + } + + /** + * 缺少参数异常 + */ + @ResponseBody + @ExceptionHandler(MissingServletRequestParameterException.class) + public AjaxResult handleMissingPathVariableException(MissingServletRequestParameterException e, HttpServletRequest request) { + String requestURI = request.getRequestURI(); + log.error("请求缺少必需的参数'" + requestURI + "',发生系统异常." + e.getMessage()); + return AjaxResult.error(ErrorUtils.ERROR, String.format("请求缺少必需的参数[%s]", e.getParameterName())); + } + + /** + * 请求参数类型不匹配 + */ + @ResponseBody + @ExceptionHandler(TypeMismatchException.class) + public AjaxResult handleMethodArgumentTypeMismatchException(TypeMismatchException e, HttpServletRequest request) { + String requestURI = request.getRequestURI(); + log.error("请求参数类型不匹配'" + requestURI + "',发生系统异常." + e.getMessage()); + return AjaxResult.error(ErrorUtils.ERROR, String.format("请求参数类型不匹配,参数[%s]要求类型为:'%s',但输入值为:'%s'", e.getPropertyName(), e.getRequiredType().getName(), e.getValue())); + } + + /** + * 拦截未知的运行时异常 + */ + @ResponseBody + @ExceptionHandler(RuntimeException.class) + public AjaxResult handleRuntimeException(RuntimeException e, HttpServletRequest request) { + String requestURI = request.getRequestURI(); + log.error("请求地址'" + requestURI + "',发生未知异常." + e.getMessage()); + return AjaxResult.error(ErrorUtils.ERROR, e.getMessage()); + } + + }