提交 0eb5196a 编写于 作者: cxt104926's avatar cxt104926

全局异常处理

上级 d65b4329
......@@ -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, "注册账号已存在");
......
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<T> implements Serializable {
//是否成功
@ApiModelProperty("是否成功")
private Boolean success;
@ApiModelProperty(value = "状态码", required = true)
......@@ -54,7 +53,7 @@ public class ApiResult<T> implements Serializable {
}
/**
* 成功:自定义消息
* 成功:自定义code,消息
*/
public static <T> ApiResult<T> success(int code, String message) {
return new ApiResult<>(true, code, message);
......@@ -89,28 +88,21 @@ public class ApiResult<T> implements Serializable {
}
/**
* 失败
*/
public static <T> ApiResult<T> fail() {
return new ApiResult<>(false, 999, "失败");
}
/**
* 失败:自定义消息
* 错误
*/
public static <T> ApiResult<T> fail(String message) {
return new ApiResult<>(false, 999, message);
public static <T> ApiResult<T> error() {
return new ApiResult<T>(false, 500, "");
}
/**
* 错误
* 错误:自定义消息
*/
public static <T> ApiResult<T> error(CommonEnum e) {
return new ApiResult<T>(false, 500, e.getResultMsg());
public static <T> ApiResult<T> error(ApiException e) {
return new ApiResult<T>(false, 500, e.getMessage());
}
/**
* 错误
* 错误:自定义code和消息
*/
public static <T> ApiResult<T> error(Integer code, String message) {
return new ApiResult<T>(false, code, message);
......
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
* <p>
* 全局异常捕获
*/
@Slf4j
@ControllerAdvice
public class OverallException {
/**
* 处理自定义的业务异常
*/
@ExceptionHandler(value = ApiException.class)
@ResponseBody
public ApiResult<ApiException> custom(HttpServletRequest req, ApiException e) {
log.error("发生错误!原因是:{}", e.getMessage());
return ApiResult.error(e.getCode(), e.getMessage());
}
}
package com.stu.stusystem.exception;
/**
* 异常信息接口
*/
public interface BaseErrorInfoInterface {
/**
* 错误码
*/
Integer getResultCode();
/**
* 错误描述
*/
String getResultMsg();
}
\ No newline at end of file
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
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;
}
}
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);
}
}
......@@ -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("验证码错误");
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册