feat(exception): 完善全局异常处理器功能
- 添加对 HttpRequestMethodNotSupportedException、MissingServletRequestParameterException 和 TypeMismatchException 的处理 -增加未知运行时异常的处理 - 优化异常处理逻辑,统一错误信息格式 - 引入 HttpServletRequest 以获取请求路径信息
This commit is contained in:
		
							parent
							
								
									fc7e6b2b5e
								
							
						
					
					
						commit
						d696a6c205
					
				|  | @ -5,13 +5,18 @@ import com.yb.lb.common.utils.ErrorUtils; | ||||||
| import com.yb.lb.common.utils.StringUtils; | import com.yb.lb.common.utils.StringUtils; | ||||||
| import org.slf4j.Logger; | import org.slf4j.Logger; | ||||||
| import org.slf4j.LoggerFactory; | import org.slf4j.LoggerFactory; | ||||||
|  | import org.springframework.beans.TypeMismatchException; | ||||||
| import org.springframework.stereotype.Component; | import org.springframework.stereotype.Component; | ||||||
| import org.springframework.validation.BindException; | import org.springframework.validation.BindException; | ||||||
|  | import org.springframework.web.HttpRequestMethodNotSupportedException; | ||||||
| import org.springframework.web.bind.MethodArgumentNotValidException; | 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.ControllerAdvice; | ||||||
| import org.springframework.web.bind.annotation.ExceptionHandler; | import org.springframework.web.bind.annotation.ExceptionHandler; | ||||||
| import org.springframework.web.bind.annotation.ResponseBody; | import org.springframework.web.bind.annotation.ResponseBody; | ||||||
| 
 | 
 | ||||||
|  | import javax.servlet.http.HttpServletRequest; | ||||||
|  | 
 | ||||||
| /** | /** | ||||||
|  * 全局异常处理器 |  * 全局异常处理器 | ||||||
|  * |  * | ||||||
|  | @ -72,7 +77,53 @@ public class GlobalExceptionHandler { | ||||||
|     public Object validExceptionHandler(MethodArgumentNotValidException e) { |     public Object validExceptionHandler(MethodArgumentNotValidException e) { | ||||||
|         log.error(e.getMessage(), e); |         log.error(e.getMessage(), e); | ||||||
|         String message = e.getBindingResult().getFieldError().getDefaultMessage(); |         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()); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue