mirror of
				https://github.com/continew-org/continew-admin.git
				synced 2025-10-31 00:57:13 +08:00 
			
		
		
		
	refactor: 将全局异常处理器未知异常的异常类型从 Exception 调整为 Throwable
This commit is contained in:
		| @@ -59,39 +59,6 @@ import top.charles7c.cnadmin.common.util.holder.LogContextHolder; | ||||
| @RestControllerAdvice | ||||
| public class GlobalExceptionHandler { | ||||
|  | ||||
|     /** | ||||
|      * 拦截未知的系统异常 | ||||
|      */ | ||||
|     @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) | ||||
|     @ExceptionHandler(Exception.class) | ||||
|     public R handleException(Exception e, HttpServletRequest request) { | ||||
|         log.error("请求地址 [{}],发生未知异常。", request.getRequestURI(), e); | ||||
|         LogContextHolder.setException(e); | ||||
|         return R.fail(e.getMessage()); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 拦截未知的运行时异常 | ||||
|      */ | ||||
|     @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) | ||||
|     @ExceptionHandler(RuntimeException.class) | ||||
|     public R handleRuntimeException(RuntimeException e, HttpServletRequest request) { | ||||
|         log.error("请求地址 [{}],发生系统异常。", request.getRequestURI(), e); | ||||
|         LogContextHolder.setException(e); | ||||
|         return R.fail(e.getMessage()); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 拦截业务异常 | ||||
|      */ | ||||
|     @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) | ||||
|     @ExceptionHandler(ServiceException.class) | ||||
|     public R handleServiceException(ServiceException e, HttpServletRequest request) { | ||||
|         log.error("请求地址 [{}],发生业务异常。", request.getRequestURI(), e); | ||||
|         LogContextHolder.setErrorMsg(e.getMessage()); | ||||
|         return R.fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage()); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 拦截自定义验证异常-错误请求 | ||||
|      */ | ||||
| @@ -103,18 +70,6 @@ public class GlobalExceptionHandler { | ||||
|         return R.fail(HttpStatus.BAD_REQUEST.value(), e.getMessage()); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 拦截校验异常-绑定异常 | ||||
|      */ | ||||
|     @ResponseStatus(HttpStatus.BAD_REQUEST) | ||||
|     @ExceptionHandler(BindException.class) | ||||
|     public R handleBindException(BindException e, HttpServletRequest request) { | ||||
|         log.warn("请求地址 [{}],参数验证失败。", request.getRequestURI(), e); | ||||
|         String errorMsg = StreamUtils.join(e.getAllErrors(), DefaultMessageSourceResolvable::getDefaultMessage, ","); | ||||
|         LogContextHolder.setErrorMsg(errorMsg); | ||||
|         return R.fail(HttpStatus.BAD_REQUEST.value(), errorMsg); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 拦截校验异常-违反约束异常 | ||||
|      */ | ||||
| @@ -127,6 +82,18 @@ public class GlobalExceptionHandler { | ||||
|         return R.fail(HttpStatus.BAD_REQUEST.value(), errorMsg); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 拦截校验异常-绑定异常 | ||||
|      */ | ||||
|     @ResponseStatus(HttpStatus.BAD_REQUEST) | ||||
|     @ExceptionHandler(BindException.class) | ||||
|     public R handleBindException(BindException e, HttpServletRequest request) { | ||||
|         log.warn("请求地址 [{}],参数验证失败。", request.getRequestURI(), e); | ||||
|         String errorMsg = StreamUtils.join(e.getAllErrors(), DefaultMessageSourceResolvable::getDefaultMessage, ","); | ||||
|         LogContextHolder.setErrorMsg(errorMsg); | ||||
|         return R.fail(HttpStatus.BAD_REQUEST.value(), errorMsg); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 拦截校验异常-方法参数无效异常 | ||||
|      */ | ||||
| @@ -153,17 +120,6 @@ public class GlobalExceptionHandler { | ||||
|         return R.fail(HttpStatus.BAD_REQUEST.value(), errorMsg); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 拦截校验异常-请求方式不支持异常 | ||||
|      */ | ||||
|     @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED) | ||||
|     @ExceptionHandler(HttpRequestMethodNotSupportedException.class) | ||||
|     public R handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e, HttpServletRequest request) { | ||||
|         LogContextHolder.setErrorMsg(e.getMessage()); | ||||
|         log.error("请求地址 [{}],不支持 [{}] 请求", request.getRequestURI(), e.getMethod()); | ||||
|         return R.fail(HttpStatus.METHOD_NOT_ALLOWED.value(), e.getMessage()); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 拦截文件上传异常-超过上传大小限制 | ||||
|      */ | ||||
| @@ -219,4 +175,48 @@ public class GlobalExceptionHandler { | ||||
|         log.error("请求地址 [{}],角色权限校验失败。", request.getRequestURI(), e); | ||||
|         return R.fail(HttpStatus.FORBIDDEN.value(), "没有访问权限,请联系管理员授权"); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 拦截校验异常-请求方式不支持异常 | ||||
|      */ | ||||
|     @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED) | ||||
|     @ExceptionHandler(HttpRequestMethodNotSupportedException.class) | ||||
|     public R handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e, HttpServletRequest request) { | ||||
|         LogContextHolder.setErrorMsg(e.getMessage()); | ||||
|         log.error("请求地址 [{}],不支持 [{}] 请求", request.getRequestURI(), e.getMethod()); | ||||
|         return R.fail(HttpStatus.METHOD_NOT_ALLOWED.value(), e.getMessage()); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 拦截业务异常 | ||||
|      */ | ||||
|     @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) | ||||
|     @ExceptionHandler(ServiceException.class) | ||||
|     public R handleServiceException(ServiceException e, HttpServletRequest request) { | ||||
|         log.error("请求地址 [{}],发生业务异常。", request.getRequestURI(), e); | ||||
|         LogContextHolder.setErrorMsg(e.getMessage()); | ||||
|         return R.fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage()); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 拦截未知的运行时异常 | ||||
|      */ | ||||
|     @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) | ||||
|     @ExceptionHandler(RuntimeException.class) | ||||
|     public R handleRuntimeException(RuntimeException e, HttpServletRequest request) { | ||||
|         log.error("请求地址 [{}],发生系统异常。", request.getRequestURI(), e); | ||||
|         LogContextHolder.setException(e); | ||||
|         return R.fail(e.getMessage()); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 拦截未知的系统异常 | ||||
|      */ | ||||
|     @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) | ||||
|     @ExceptionHandler(Throwable.class) | ||||
|     public R handleException(Throwable e, HttpServletRequest request) { | ||||
|         log.error("请求地址 [{}],发生未知异常。", request.getRequestURI(), e); | ||||
|         LogContextHolder.setException(e); | ||||
|         return R.fail(e.getMessage()); | ||||
|     } | ||||
| } | ||||
| @@ -50,5 +50,5 @@ public class LogContext implements Serializable { | ||||
|     /** | ||||
|      * 异常信息 | ||||
|      */ | ||||
|     private Exception exception; | ||||
|     private Throwable exception; | ||||
| } | ||||
|   | ||||
| @@ -64,9 +64,9 @@ public class LogContextHolder { | ||||
|      * @param e | ||||
|      *            异常信息 | ||||
|      */ | ||||
|     public static void setException(Exception e) { | ||||
|     public static void setException(Throwable e) { | ||||
|         LogContext logContext = get(); | ||||
|         if (logContext != null) { | ||||
|         if (null != logContext) { | ||||
|             logContext.setErrorMsg(e.getMessage()); | ||||
|             logContext.setException(e); | ||||
|         } | ||||
| @@ -80,7 +80,7 @@ public class LogContextHolder { | ||||
|      */ | ||||
|     public static void setErrorMsg(String errorMsg) { | ||||
|         LogContext logContext = get(); | ||||
|         if (logContext != null) { | ||||
|         if (null != logContext) { | ||||
|             logContext.setErrorMsg(errorMsg); | ||||
|         } | ||||
|     } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user