(1)统一异常处理
1.异常错误枚举
import lombok.AllArgsConstructor;
import lombok.Getter;
@AllArgsConstructor
@Getter
public enum ExceptionCode {
/**
* 异常信息
*/
PARAM_ERROR("8000", "填入异常信息"),
COMMON_ERROR("8001", "填入异常信息"),
OTHER_ERROR("8002", "填入异常信息");
private final String code;
private final String msg;
}
2. 配置统一异常处理类
import com.yang.demo.config.bean.ExceptionCode;
import com.yang.demo.util.ResultJson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@Slf4j
@RestControllerAdvice
public class ExceptionAdvice {
/**
* 系统异常.
*
* @param exception 异常信息
* @return R
*/
@ExceptionHandler(Exception.class)
public ResultJson exception(Exception exception) {
log.error("系统异常信息 ex={}", exception.getMessage(), exception);
// 未知异常统一抛出8000
return new ResultJson(ExceptionCode.OTHER_ERROR.getCode(), ExceptionCode.OTHER_ERROR.getMsg());
}
}
(2)自定义异常
1. 自定义异常 BaseException(用于细化自定义异常,使自定义异常继承此类)
import com.yang.demo.config.bean.ExceptionCode;
import lombok.Data;
import lombok.EqualsAndHashCode;
@Data
@EqualsAndHashCode(callSuper = true)
public class BaseException extends RuntimeException {
private final String code;
private final String errorMsg;
public BaseException(String code, String errorMsg) {
super(errorMsg);
this.code = code;
this.errorMsg = errorMsg;
}
public BaseException(ExceptionCode code) {
super(code.getMsg());
this.code = code.getCode();
this.errorMsg = code.getMsg();
}
public BaseException(ExceptionCode code, String errorMsg) {
super(errorMsg);
this.code = code.getCode();
this.errorMsg = errorMsg;
}
}
2. 编写异常类继承BaseException
import com.yang.demo.config.bean.ExceptionCode;
public class CheckException extends BaseException {
public CheckException(String code, String errorMsg) {
super(code, errorMsg);
}
public CheckException(ExceptionCode code) {
super(code);
}
public CheckException(ExceptionCode code, String errorMsg) {
super(code, errorMsg);
}
}
3. 添加方法
import com.yang.demo.config.bean.ExceptionCode;
import com.yang.demo.util.ResultJson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@Slf4j
@RestControllerAdvice
public class ExceptionAdvice {
/**
* 系统异常.
*
* @param exception 异常信息
* @return R
*/
@ExceptionHandler(BaseException.class)
@Order(99)
public ResultJson checkException(BaseException exception) {
log.error("系统异常信息 ex={}", exception.getMessage(), exception);
// 未知异常统一抛出9999
return new ResultJson(ExceptionCode.OTHER_ERROR.getCode(), ExceptionCode.OTHER_ERROR.getMsg());
}
/**
* 系统异常.
*
* @param exception 异常信息
* @return R
*/
@ExceptionHandler(Exception.class)
@Order(99)
public ResultJson checkException(Exception exception) {
log.error("系统异常信息 ex={}", exception.getMessage(), exception);
// 未知异常统一抛出9999
return new ResultJson(ExceptionCode.OTHER_ERROR.getCode(), ExceptionCode.OTHER_ERROR.getMsg());
}
}