From 0eb5196a127ec916e79e7ab495ef52b4700d7bc4 Mon Sep 17 00:00:00 2001 From: cxt104926 <1049266034@qq.com> Date: Thu, 11 Mar 2021 00:37:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=A8=E5=B1=80=E5=BC=82=E5=B8=B8=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../stu/stusystem/common/ApiException.java | 9 +-- .../com/stu/stusystem/common/ApiResult.java | 26 +++--- .../stusystem/common/OverallException.java | 29 +++++++ .../exception/BaseErrorInfoInterface.java | 16 ---- .../stu/stusystem/exception/BizException.java | 80 ------------------- .../stu/stusystem/exception/CommonEnum.java | 40 ---------- .../exception/GlobalExceptionHandler.java | 59 -------------- .../stu/stusystem/service/LoginService.java | 1 - 8 files changed, 41 insertions(+), 219 deletions(-) create mode 100644 src/main/java/com/stu/stusystem/common/OverallException.java delete mode 100644 src/main/java/com/stu/stusystem/exception/BaseErrorInfoInterface.java delete mode 100644 src/main/java/com/stu/stusystem/exception/BizException.java delete mode 100644 src/main/java/com/stu/stusystem/exception/CommonEnum.java delete mode 100644 src/main/java/com/stu/stusystem/exception/GlobalExceptionHandler.java diff --git a/src/main/java/com/stu/stusystem/common/ApiException.java b/src/main/java/com/stu/stusystem/common/ApiException.java index 5b7ee1c..ee8da2b 100644 --- a/src/main/java/com/stu/stusystem/common/ApiException.java +++ b/src/main/java/com/stu/stusystem/common/ApiException.java @@ -10,9 +10,11 @@ public class ApiException extends RuntimeException { @Getter @Setter private Integer code; + @Getter @Setter private String message; + @Getter @Setter private Object data; @@ -22,11 +24,6 @@ public class ApiException extends RuntimeException { this.message = e.getMessage(); } - public ApiException(ApiResult e) { - this.code = e.getCode(); - this.message = e.getMessage(); - } - /** * 系统错误 */ @@ -38,7 +35,7 @@ public class ApiException extends RuntimeException { * 提示 */ public static final ErrorVM E_LOGIN_UNKNOWN_EMPTY = new ErrorVM(-10001, "验证码不能为空"); - public static final ApiResult E_LOGIN_UNKNOWN_CODE = new ApiResult(-10002, "验证码错误"); + public static final ErrorVM E_LOGIN_UNKNOWN_CODE = new ErrorVM(-10002, "验证码错误"); public static final ErrorVM E_LOGIN_INCORRECT_CREDENTIAL = new ErrorVM(-10003, "账号或者密码错误"); public static final ErrorVM E_LOGIN_EXCESSIVE_ATTEMPTS = new ErrorVM(-10004, "错误次数过多"); public static final ErrorVM E_REGISTER_EXISTENCE_ACCOUNT = new ErrorVM(-10006, "注册账号已存在"); diff --git a/src/main/java/com/stu/stusystem/common/ApiResult.java b/src/main/java/com/stu/stusystem/common/ApiResult.java index 1b20df9..71db3ca 100644 --- a/src/main/java/com/stu/stusystem/common/ApiResult.java +++ b/src/main/java/com/stu/stusystem/common/ApiResult.java @@ -1,6 +1,5 @@ package com.stu.stusystem.common; -import com.stu.stusystem.exception.CommonEnum; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.*; @@ -22,7 +21,7 @@ import java.io.Serializable; @ApiModel(description = "统一响应消息") public class ApiResult implements Serializable { - //是否成功 + @ApiModelProperty("是否成功") private Boolean success; @ApiModelProperty(value = "状态码", required = true) @@ -54,7 +53,7 @@ public class ApiResult implements Serializable { } /** - * 成功:自定义消息 + * 成功:自定义code,消息 */ public static ApiResult success(int code, String message) { return new ApiResult<>(true, code, message); @@ -89,28 +88,21 @@ public class ApiResult implements Serializable { } /** - * 失败 - */ - public static ApiResult fail() { - return new ApiResult<>(false, 999, "失败"); - } - - /** - * 失败:自定义消息 + * 错误 */ - public static ApiResult fail(String message) { - return new ApiResult<>(false, 999, message); + public static ApiResult error() { + return new ApiResult(false, 500, ""); } /** - * 错误 + * 错误:自定义消息 */ - public static ApiResult error(CommonEnum e) { - return new ApiResult(false, 500, e.getResultMsg()); + public static ApiResult error(ApiException e) { + return new ApiResult(false, 500, e.getMessage()); } /** - * 错误 + * 错误:自定义code和消息 */ public static ApiResult error(Integer code, String message) { return new ApiResult(false, code, message); diff --git a/src/main/java/com/stu/stusystem/common/OverallException.java b/src/main/java/com/stu/stusystem/common/OverallException.java new file mode 100644 index 0000000..395e289 --- /dev/null +++ b/src/main/java/com/stu/stusystem/common/OverallException.java @@ -0,0 +1,29 @@ +package com.stu.stusystem.common; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; + +import javax.servlet.http.HttpServletRequest; + +/** + * @author: cxt + * @time: 2021/3/10 + *

+ * 全局异常捕获 + */ +@Slf4j +@ControllerAdvice +public class OverallException { + + /** + * 处理自定义的业务异常 + */ + @ExceptionHandler(value = ApiException.class) + @ResponseBody + public ApiResult custom(HttpServletRequest req, ApiException e) { + log.error("发生错误!原因是:{}", e.getMessage()); + return ApiResult.error(e.getCode(), e.getMessage()); + } +} diff --git a/src/main/java/com/stu/stusystem/exception/BaseErrorInfoInterface.java b/src/main/java/com/stu/stusystem/exception/BaseErrorInfoInterface.java deleted file mode 100644 index 087d3c9..0000000 --- a/src/main/java/com/stu/stusystem/exception/BaseErrorInfoInterface.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.stu.stusystem.exception; - -/** - * 异常信息接口 - */ -public interface BaseErrorInfoInterface { - /** - * 错误码 - */ - Integer getResultCode(); - - /** - * 错误描述 - */ - String getResultMsg(); -} \ No newline at end of file diff --git a/src/main/java/com/stu/stusystem/exception/BizException.java b/src/main/java/com/stu/stusystem/exception/BizException.java deleted file mode 100644 index 551a93a..0000000 --- a/src/main/java/com/stu/stusystem/exception/BizException.java +++ /dev/null @@ -1,80 +0,0 @@ -package com.stu.stusystem.exception; - -/** - * @author: cxt - * @time: 2021/3/10 - * 自定义异常类 - */ -public class BizException extends RuntimeException { - - private static final long serialVersionUID = 1L; - - /** - * 错误码 - */ - protected String errorCode; - /** - * 错误信息 - */ - protected String errorMsg; - - public BizException() { - super(); - } - - public BizException(BaseErrorInfoInterface errorInfoInterface) { - super(errorInfoInterface.getResultCode()); - this.errorCode = errorInfoInterface.getResultCode(); - this.errorMsg = errorInfoInterface.getResultMsg(); - } - - public BizException(BaseErrorInfoInterface errorInfoInterface, Throwable cause) { - super(errorInfoInterface.getResultCode(), cause); - this.errorCode = errorInfoInterface.getResultCode(); - this.errorMsg = errorInfoInterface.getResultMsg(); - } - - public BizException(String errorMsg) { - super(errorMsg); - this.errorMsg = errorMsg; - } - - public BizException(String errorCode, String errorMsg) { - super(errorCode); - this.errorCode = errorCode; - this.errorMsg = errorMsg; - } - - public BizException(String errorCode, String errorMsg, Throwable cause) { - super(errorCode, cause); - this.errorCode = errorCode; - this.errorMsg = errorMsg; - } - - - public String getErrorCode() { - return errorCode; - } - - public void setErrorCode(String errorCode) { - this.errorCode = errorCode; - } - - public String getErrorMsg() { - return errorMsg; - } - - public void setErrorMsg(String errorMsg) { - this.errorMsg = errorMsg; - } - - public String getMessage() { - return errorMsg; - } - - @Override - public Throwable fillInStackTrace() { - return this; - } - -} \ No newline at end of file diff --git a/src/main/java/com/stu/stusystem/exception/CommonEnum.java b/src/main/java/com/stu/stusystem/exception/CommonEnum.java deleted file mode 100644 index 890f02d..0000000 --- a/src/main/java/com/stu/stusystem/exception/CommonEnum.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.stu.stusystem.exception; - -/** - * 异常枚举类 - */ -public enum CommonEnum implements BaseErrorInfoInterface { - // 数据操作错误定义 - SUCCESS("200", "成功!"), - BODY_NOT_MATCH("400", "请求的数据格式不符!"), - SIGNATURE_NOT_MATCH("401", "请求的数字签名不匹配!"), - NOT_FOUND("404", "未找到该资源!"), - INTERNAL_SERVER_ERROR("500", "服务器内部错误!"), - SERVER_BUSY("503", "服务器正忙,请稍后再试!"); - - /** - * 错误码 - */ - private String resultCode; - - /** - * 错误描述 - */ - private String resultMsg; - - CommonEnum(String resultCode, String resultMsg) { - this.resultCode = resultCode; - this.resultMsg = resultMsg; - } - - @Override - public String getResultCode() { - return resultCode; - } - - @Override - public String getResultMsg() { - return resultMsg; - } - -} diff --git a/src/main/java/com/stu/stusystem/exception/GlobalExceptionHandler.java b/src/main/java/com/stu/stusystem/exception/GlobalExceptionHandler.java deleted file mode 100644 index f8493bf..0000000 --- a/src/main/java/com/stu/stusystem/exception/GlobalExceptionHandler.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.stu.stusystem.exception; - -import com.stu.stusystem.common.ApiResult; -import lombok.extern.slf4j.Slf4j; -import org.springframework.web.bind.annotation.ControllerAdvice; -import org.springframework.web.bind.annotation.ExceptionHandler; -import org.springframework.web.bind.annotation.ResponseBody; - -import javax.servlet.http.HttpServletRequest; - -/** - * @author: cxt - * @time: 2021/3/10 - * 全局异常处理 - */ -@ControllerAdvice -@Slf4j -public class GlobalExceptionHandler { - - /** - * 处理自定义的业务异常 - * @param req - * @param e - * @return - */ - @ExceptionHandler(value = BizException.class) - @ResponseBody - public ApiResult bizExceptionHandler(HttpServletRequest req, BizException e){ - log.error("发生业务异常!原因是:{}",e.getErrorMsg()); - return ApiResult.error(Integer.parseInt(e.getErrorCode()),e.getErrorMsg()); - } - - /** - * 处理空指针的异常 - * @param req - * @param e - * @return - */ - @ExceptionHandler(value =NullPointerException.class) - @ResponseBody - public ApiResult exceptionHandler(HttpServletRequest req, NullPointerException e){ - log.error("发生空指针异常!原因是:",e); - return ApiResult.error(CommonEnum.BODY_NOT_MATCH); - } - - - /** - * 处理其他异常 - * @param req - * @param e - * @return - */ - @ExceptionHandler(value =Exception.class) - @ResponseBody - public ApiResult exceptionHandler(HttpServletRequest req, Exception e){ - log.error("未知异常!原因是:",e); - return ApiResult.error(CommonEnum.INTERNAL_SERVER_ERROR); - } -} diff --git a/src/main/java/com/stu/stusystem/service/LoginService.java b/src/main/java/com/stu/stusystem/service/LoginService.java index 9c171c7..43b4931 100644 --- a/src/main/java/com/stu/stusystem/service/LoginService.java +++ b/src/main/java/com/stu/stusystem/service/LoginService.java @@ -76,7 +76,6 @@ public class LoginService { String v = newCode.toLowerCase(); if (!v.equals(c)) { throw new ApiException(ApiException.E_LOGIN_UNKNOWN_CODE); -// throw new ApiException("验证码错误"); } } -- GitLab