diff --git a/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/handler/GlobalExceptionHandler.java b/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/handler/GlobalExceptionHandler.java index feb419f13b318e1e4fb5039acb0d212f716acbb5..a3118d4ccc548554639bcb1f14f53404bf7e2d34 100644 --- a/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/handler/GlobalExceptionHandler.java +++ b/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/handler/GlobalExceptionHandler.java @@ -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()); - } - /** * 拦截自定义验证异常-错误请求 */ @@ -104,25 +71,25 @@ public class GlobalExceptionHandler { } /** - * 拦截校验异常-绑定异常 + * 拦截校验异常-违反约束异常 */ @ResponseStatus(HttpStatus.BAD_REQUEST) - @ExceptionHandler(BindException.class) - public R handleBindException(BindException e, HttpServletRequest request) { + @ExceptionHandler(ConstraintViolationException.class) + public R constraintViolationException(ConstraintViolationException e, HttpServletRequest request) { log.warn("请求地址 [{}],参数验证失败。", request.getRequestURI(), e); - String errorMsg = StreamUtils.join(e.getAllErrors(), DefaultMessageSourceResolvable::getDefaultMessage, ","); + String errorMsg = StreamUtils.join(e.getConstraintViolations(), ConstraintViolation::getMessage, ","); LogContextHolder.setErrorMsg(errorMsg); return R.fail(HttpStatus.BAD_REQUEST.value(), errorMsg); } /** - * 拦截校验异常-违反约束异常 + * 拦截校验异常-绑定异常 */ @ResponseStatus(HttpStatus.BAD_REQUEST) - @ExceptionHandler(ConstraintViolationException.class) - public R constraintViolationException(ConstraintViolationException e, HttpServletRequest request) { + @ExceptionHandler(BindException.class) + public R handleBindException(BindException e, HttpServletRequest request) { log.warn("请求地址 [{}],参数验证失败。", request.getRequestURI(), e); - String errorMsg = StreamUtils.join(e.getConstraintViolations(), ConstraintViolation::getMessage, ","); + 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()); + } } \ No newline at end of file diff --git a/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/model/dto/LogContext.java b/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/model/dto/LogContext.java index bd591c379c2781cef781ced8583bb4601345df6a..e2a4a2148f42d5421dbab3f8745b8f514316bd44 100644 --- a/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/model/dto/LogContext.java +++ b/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/model/dto/LogContext.java @@ -50,5 +50,5 @@ public class LogContext implements Serializable { /** * 异常信息 */ - private Exception exception; + private Throwable exception; } diff --git a/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/util/holder/LogContextHolder.java b/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/util/holder/LogContextHolder.java index d01ec3f40e7b4d7eeffdb0a522f7e3dc6ac98967..a12a4606494001cd50161fe0a2561711875291af 100644 --- a/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/util/holder/LogContextHolder.java +++ b/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/util/holder/LogContextHolder.java @@ -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); } } diff --git a/continew-admin-monitor/src/main/java/top/charles7c/cnadmin/monitor/interceptor/LogInterceptor.java b/continew-admin-monitor/src/main/java/top/charles7c/cnadmin/monitor/interceptor/LogInterceptor.java index c2e23d2fc97320140eca95fb0d313899723947ea..dea1d5d6c3b758fa10393e2c486f3eb8dfc378c2 100644 --- a/continew-admin-monitor/src/main/java/top/charles7c/cnadmin/monitor/interceptor/LogInterceptor.java +++ b/continew-admin-monitor/src/main/java/top/charles7c/cnadmin/monitor/interceptor/LogInterceptor.java @@ -141,7 +141,7 @@ public class LogInterceptor implements HandlerInterceptor { logDO.setErrorMsg(errorMsg); } // 记录异常详情 - Exception exception = logContext.getException(); + Throwable exception = logContext.getException(); if (null != exception) { logDO.setStatus(LogStatusEnum.FAILURE); logDO.setExceptionDetail(ExceptionUtil.stacktraceToString(exception, -1));