diff --git a/xtools-boot-web/xtools-boot-web-base/src/main/java/xtools/boot/web/base/exception/GlobalControllerExceptionHandler.java b/xtools-boot-web/xtools-boot-web-base/src/main/java/xtools/boot/web/base/exception/GlobalControllerExceptionHandler.java index 13aad0d..8df2b38 100644 --- a/xtools-boot-web/xtools-boot-web-base/src/main/java/xtools/boot/web/base/exception/GlobalControllerExceptionHandler.java +++ b/xtools-boot-web/xtools-boot-web-base/src/main/java/xtools/boot/web/base/exception/GlobalControllerExceptionHandler.java @@ -22,6 +22,7 @@ import xtools.boot.api.model.dto.log.LogTrack; import xtools.boot.log.LogBus; import xtools.boot.log.enums.LogBusBaseType; import xtools.boot.log.holder.LogTrackHolder; +import xtools.boot.log.interfaces.LogBusType; import xtools.core.StringUtils; import xtools.core.enums.LogLevel; import xtools.core.extend.TemplateUtils; @@ -59,7 +60,7 @@ public class GlobalControllerExceptionHandler implements BaseParams { @ExceptionHandler(MethodArgumentTypeMismatchException.class) public Result handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e, HttpServletRequest request) { String errMsg = TemplateUtils.format(ARGUMENT_TYPE_MISMATCH_TEMPLATE, e.getPropertyName()); - logException("参数类型不匹配", errMsg, request, e); + debugLog("参数类型不匹配", errMsg, request, e); return Result.badRequest(errMsg); } @@ -82,7 +83,7 @@ public class GlobalControllerExceptionHandler implements BaseParams { validation.getResolvableErrors().forEach(item -> msg.add(paramName + CP_COLON + item.getDefaultMessage())); }); String errMsg = msg.toString(); - logException("参数校验异常", errMsg, request, e); + debugLog("参数校验异常", errMsg, request, e); return Result.badRequest(errMsg); } @@ -104,7 +105,7 @@ public class GlobalControllerExceptionHandler implements BaseParams { msg.add(fieldName + CP_COLON + errorMessage); }); String errMsg = msg.toString(); - logException("参数校验异常", errMsg, request, e); + debugLog("参数校验异常", errMsg, request, e); return Result.badRequest(errMsg); } @@ -118,62 +119,10 @@ public class GlobalControllerExceptionHandler implements BaseParams { @ExceptionHandler(HttpMessageNotReadableException.class) public Result handleHttpMessageNotReadableException(HttpMessageNotReadableException e, HttpServletRequest request) { String errMsg = "请求参数格式错误"; - logException(errMsg, e.getMessage(), request, e); + debugLog(errMsg, e.getMessage(), request, e); return Result.badRequest(errMsg); } - /** - * 请求方法不支持 - * - * @param e 请求方法不支持异常 - * @param request 请求 - * @return 错误结果 - */ - @ExceptionHandler(HttpRequestMethodNotSupportedException.class) - public Result handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e, HttpServletRequest request) { - logException("请求方法不支持", null, request, e); - return new Result<>(ResultType.METHOD_NOT_ALLOWED, null); - } - - /** - * 资源未找到 - * - * @param e 资源未找到异常 - * @param request 请求 - * @return 错误结果 - */ - @ExceptionHandler(NoResourceFoundException.class) - public Result handleNoResourceFoundException(NoResourceFoundException e, HttpServletRequest request) { - logException("资源未找到", null, request, e); - return new Result<>(ResultType.NOT_FOUND, null); - } - - /** - * 客户端提前断开连接 - * - * @param e 客户端提前断开连接异常 - * @param request 请求 - */ - @ExceptionHandler(ClientAbortException.class) - public void handleClientAbortException(ClientAbortException e, HttpServletRequest request) { - String title = "客户端提前断开连接"; - LogBus.init(LogLevel.WARN, LogBusBaseType.HTTP_RESPONSE).print(false).error(e).title(title).save(); - logException(title, e.getMessage(), request, e); - } - - /** - * 认证异常 - * - * @param e 认证异常 - * @param request 请求 - * @return 错误结果 - */ - @ExceptionHandler(UnauthorizedError.class) - public Result handleUnauthorizedErrorException(UnauthorizedError e, HttpServletRequest request) { - logException("认证异常", e.getMessage(), request, e); - return new Result<>(ResultType.UNAUTHORIZED, null); - } - /** * 公钥错误 * @@ -183,7 +132,7 @@ public class GlobalControllerExceptionHandler implements BaseParams { */ @ExceptionHandler(BizPublicKeyError.class) public Result handleBizPublicKeyErrorException(BizPublicKeyError e, HttpServletRequest request) { - logException("公钥错误", e.getMessage(), request, e); + debugLog("公钥错误", e.getMessage(), request, e); return new Result<>(true, e.getCode(), e.getMessage(), null); } @@ -196,7 +145,7 @@ public class GlobalControllerExceptionHandler implements BaseParams { */ @ExceptionHandler(BizError.class) public Result handleBizErrorException(BizError e, HttpServletRequest request) { - logException("业务异常", e.getMessage(), request, e); + debugLog("业务异常", e.getMessage(), request, e); return new Result<>(true, e.getCode(), e.getMessage(), null); } @@ -209,38 +158,111 @@ public class GlobalControllerExceptionHandler implements BaseParams { */ @ExceptionHandler(BizWarning.class) public Result handleBizWarningException(BizWarning e, HttpServletRequest request) { - logException("业务警告", e.getMessage(), request, e); + debugLog("业务警告", e.getMessage(), request, e); return new Result<>(true, e.getCode(), e.getMessage(), null); } + /** + * 认证异常 + * + * @param e 认证异常 + * @return 错误结果 + */ + @ExceptionHandler(UnauthorizedError.class) + public Result handleUnauthorizedErrorException(UnauthorizedError e) { + errorLog(LogLevel.ERROR, LogBusBaseType.HTTP_REQUEST, "认证异常", e); + return new Result<>(ResultType.UNAUTHORIZED, null); + } + + /** + * 请求方法不支持 + * + * @param e 请求方法不支持异常 + * @return 错误结果 + */ + @ExceptionHandler(HttpRequestMethodNotSupportedException.class) + public Result handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) { + errorLog(LogLevel.WARN, LogBusBaseType.HTTP_REQUEST, "请求方法不支持", e); + return new Result<>(ResultType.METHOD_NOT_ALLOWED, null); + } + + /** + * 资源未找到 + * + * @param e 资源未找到异常 + * @return 错误结果 + */ + @ExceptionHandler(NoResourceFoundException.class) + public Result handleNoResourceFoundException(NoResourceFoundException e) { + errorLog(LogLevel.WARN, LogBusBaseType.HTTP_REQUEST, "资源未找到", e, false); + return new Result<>(ResultType.NOT_FOUND, null); + } + + /** + * 客户端提前断开连接 + * + * @param e 客户端提前断开连接异常 + */ + @ExceptionHandler(ClientAbortException.class) + public void handleClientAbortException(ClientAbortException e) { + errorLog(LogLevel.WARN, LogBusBaseType.HTTP_REQUEST, "客户端提前断开连接", e, false); + } + /** * 默认全局异常处理 * - * @param e 异常 - * @param request 请求 + * @param e 异常 * @return 错误结果 */ @ExceptionHandler(Exception.class) - public Result handleException(Exception e, HttpServletRequest request) { - logException("默认全局异常处理", null, request, e); - LogTrack logTrack = LogTrackHolder.getDefNull(); - if (Objects.nonNull(logTrack)) { - LogBus.init(LogLevel.ERROR, LogBusBaseType.CONTROLLER, logTrack).title("默认全局异常处理").error(e).save(); - } else { - log.error("默认全局异常处理", e); - } + public Result handleException(Exception e) { + errorLog(LogLevel.ERROR, LogBusBaseType.CONTROLLER, "默认全局异常处理", e); return new Result<>(ResultType.INTERNAL_SERVER_ERROR, null); } /** - * 日志异常 + * 错误日志 + * + * @param level 日志级别 + * @param type 异常类型 + * @param title 标题 + * @param e 异常 + */ + private void errorLog(LogLevel level, LogBusType type, String title, Exception e) { + errorLog(level, type, title, e, null); + } + + /** + * 错误日志 + * + * @param level 日志级别 + * @param type 异常类型 + * @param title 标题 + * @param e 异常 + * @param print 是否打印 + */ + private void errorLog(LogLevel level, LogBusType type, String title, Exception e, Boolean print) { + LogTrack logTrack = LogTrackHolder.getDefNull(); + if (Objects.nonNull(logTrack)) { + LogBus logBus = LogBus.init(level, type, logTrack).title(title).error(e); + if (Objects.nonNull(print)) { + logBus.print(print); + } + logBus.save(); + } else { + log.error(title, e); + } + } + + /** + * 调试日志 * * @param type 异常类型 * @param msg 错误消息 * @param request 请求 * @param e 异常 */ - private void logException(String type, String msg, HttpServletRequest request, Exception e) { + private void debugLog(String type, String msg, HttpServletRequest request, Exception e) { if (StringUtils.isBlank(msg)) { log.debug("请求[{}]异常,类型[{}]", request.getRequestURI(), type, e); } else {