From d675d0725474e5e450ccd4c91ff36cc0085b514d Mon Sep 17 00:00:00 2001 From: zhuyijun Date: Tue, 20 Sep 2022 23:34:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Doauth-starter=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/entity/response/HttpCode.java | 5 +++ .../entity/response/ResponseResult.java | 17 +++++----- .../web/hander/GlobalExceptionHandler.java | 32 ++++++++----------- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/stater/zyjblogs-common-spring-boot-starter/src/main/java/cn/zyjblogs/starter/common/entity/response/HttpCode.java b/stater/zyjblogs-common-spring-boot-starter/src/main/java/cn/zyjblogs/starter/common/entity/response/HttpCode.java index 08d37d6..26186f5 100644 --- a/stater/zyjblogs-common-spring-boot-starter/src/main/java/cn/zyjblogs/starter/common/entity/response/HttpCode.java +++ b/stater/zyjblogs-common-spring-boot-starter/src/main/java/cn/zyjblogs/starter/common/entity/response/HttpCode.java @@ -1,5 +1,9 @@ package cn.zyjblogs.starter.common.entity.response; +/** + * @author zhuyijun + */ + public enum HttpCode { CONTINUE(100, "Continue"), SWITCHING_PROTOCOLS(101, "Switching Protocols"), @@ -128,6 +132,7 @@ public enum HttpCode { return this.is4xxClientError() || this.is5xxServerError(); } + @Override public String toString() { return this.value + " " + this.name(); } diff --git a/stater/zyjblogs-common-spring-boot-starter/src/main/java/cn/zyjblogs/starter/common/entity/response/ResponseResult.java b/stater/zyjblogs-common-spring-boot-starter/src/main/java/cn/zyjblogs/starter/common/entity/response/ResponseResult.java index d510ad1..f74bd28 100644 --- a/stater/zyjblogs-common-spring-boot-starter/src/main/java/cn/zyjblogs/starter/common/entity/response/ResponseResult.java +++ b/stater/zyjblogs-common-spring-boot-starter/src/main/java/cn/zyjblogs/starter/common/entity/response/ResponseResult.java @@ -5,6 +5,9 @@ package cn.zyjblogs.starter.common.entity.response; +/** + * @author zhuyijun + */ public class ResponseResult { private static final int ZERO_DATA_TOTAL = 0; private static final int ONE_DATA_TOTAL = 1; @@ -23,22 +26,20 @@ public class ResponseResult { public static ResponseObject success(long total, T data) { return ResponseObject.builder().code(HttpCode.OK.value()).msg(HttpCode.OK.getReasonPhrase()).count(total).data(data).timestamp(System.currentTimeMillis()).build(); } + public static ResponseObject error(HttpCode responseCode, String errorMsg) { - return error(responseCode, responseCode.getReasonPhrase(), errorMsg, 0L, null); - } - public static ResponseObject error(HttpCode responseCode, String errorCode, String errorMsg) { - return error(responseCode, errorCode, errorMsg, 0L, null); + return error(responseCode, errorMsg, 0L, null); } - public static ResponseObject error(HttpCode responseCode, String errorCode, String errorMsg, T data) { - return error(responseCode, errorCode, errorMsg, 1L, data); + public static ResponseObject error(HttpCode responseCode, String errorMsg, T data) { + return error(responseCode, errorMsg, 1L, data); } - public static ResponseObject error(HttpCode responseCode, String errorCode, String errorMsg, long total, T data) { + public static ResponseObject error(HttpCode responseCode, String errorMsg, long total, T data) { if (responseCode.value() == HttpCode.OK.value()) { throw new IllegalArgumentException("ResponseResult.error方法中的responseCode参数不能是ResponseCode.REQUEST_SUCCESS"); } else { - return ResponseObject.builder().code(responseCode.value()).errorCode(errorCode).msg(errorMsg == null ? responseCode.getReasonPhrase() : errorMsg).count(total).data(data).timestamp(System.currentTimeMillis()).build(); + return ResponseObject.builder().code(responseCode.value()).msg(errorMsg == null ? responseCode.getReasonPhrase() : errorMsg).count(total).data(data).timestamp(System.currentTimeMillis()).build(); } } } diff --git a/stater/zyjblogs-web-spring-boot-starter/src/main/java/cn/zyjblogs/starter/web/hander/GlobalExceptionHandler.java b/stater/zyjblogs-web-spring-boot-starter/src/main/java/cn/zyjblogs/starter/web/hander/GlobalExceptionHandler.java index 35ffe0c..e6c4e76 100644 --- a/stater/zyjblogs-web-spring-boot-starter/src/main/java/cn/zyjblogs/starter/web/hander/GlobalExceptionHandler.java +++ b/stater/zyjblogs-web-spring-boot-starter/src/main/java/cn/zyjblogs/starter/web/hander/GlobalExceptionHandler.java @@ -8,6 +8,7 @@ package cn.zyjblogs.starter.web.hander; import cn.zyjblogs.starter.common.entity.response.HttpCode; import cn.zyjblogs.starter.common.entity.response.ResponseObject; import cn.zyjblogs.starter.common.entity.response.ResponseResult; +import cn.zyjblogs.starter.common.exception.AbstractBusinessException; import cn.zyjblogs.starter.common.exception.AbstractFrameworkException; import java.util.List; import java.util.Set; @@ -66,7 +67,6 @@ public class GlobalExceptionHandler { @ExceptionHandler({MethodArgumentTypeMismatchException.class}) public ResponseObject handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException exception) { - String parameterName = exception.getParameter().getParameterName(); String i18nCode = "web_starter_property_format_error"; return ResponseResult.error(HttpCode.BAD_REQUEST, i18nCode, i18nCode); } @@ -80,7 +80,7 @@ public class GlobalExceptionHandler { i18nCode = "web_starter_bad_request"; return ResponseResult.error(HttpCode.BAD_REQUEST, i18nCode, i18nCode); } else { - i18nCode = ((ConstraintViolation)constraintViolations.stream().findFirst().get()).getMessage(); + i18nCode = constraintViolations.stream().findFirst().get().getMessage(); return ResponseResult.error(HttpCode.BAD_REQUEST, i18nCode, i18nCode); } } @@ -94,10 +94,13 @@ public class GlobalExceptionHandler { @ExceptionHandler({AbstractFrameworkException.class}) public ResponseObject handleFrameworkException(AbstractFrameworkException exception) { log.error("系统异常:", exception); - String i18nCode = "web_starter_internal_server_error"; - return ResponseResult.error(HttpCode.INTERNAL_SERVER_ERROR, i18nCode, i18nCode); + return ResponseResult.error(HttpCode.INTERNAL_SERVER_ERROR, exception.getMessage()); + } + @ExceptionHandler({AbstractBusinessException.class}) + public ResponseObject handleBusinessException(AbstractBusinessException exception) { + log.error("系统异常:", exception); + return ResponseResult.error(HttpCode.INTERNAL_SERVER_ERROR, exception.getMessage()); } - @ExceptionHandler({BindException.class}) public ResponseObject handleBindException(BindException exception) { BindingResult bindingResult = exception.getBindingResult(); @@ -107,26 +110,17 @@ public class GlobalExceptionHandler { @ExceptionHandler({HttpMessageNotReadableException.class}) public ResponseObject handleHttpMessageNotReadableException(HttpMessageNotReadableException exception) { log.error("参数格式不正确", exception); - String i18nCode = "web_starter_param_format_error"; - return ResponseResult.error(HttpCode.BAD_REQUEST, i18nCode, i18nCode); + return ResponseResult.error(HttpCode.BAD_REQUEST, exception.getMessage()); } private ResponseObject getByBindingResult(BindingResult bindingResult) { List fieldErrors = bindingResult.getFieldErrors(); if (fieldErrors.isEmpty()) { - log.error("捕获到了参数校验错误,但是没有找到错误提示的i18nCode,{}", bindingResult); - String i18nCode = "web_starter_parameter_error"; - return ResponseResult.error(HttpCode.BAD_REQUEST, i18nCode, i18nCode); + log.error("捕获到了参数校验错误,{}", bindingResult); + return ResponseResult.error(HttpCode.BAD_REQUEST, bindingResult.toString()); } else { - FieldError fieldError = (FieldError)fieldErrors.get(0); - String i18nCode; - if ("typeMismatch".equals(fieldError.getCode())) { - i18nCode = "web_starter_property_format_error"; - return ResponseResult.error(HttpCode.BAD_REQUEST, i18nCode, i18nCode); - } else { - i18nCode = fieldError.getDefaultMessage(); - return ResponseResult.error(HttpCode.BAD_REQUEST, i18nCode, i18nCode); - } + FieldError fieldError = fieldErrors.get(0); + return ResponseResult.error(HttpCode.BAD_REQUEST, fieldError.getDefaultMessage()); } } }