自定义异常处理

Author Avatar
ciky 08月 21,2024
  • 在其它设备中阅读本文章
  • 点击生成二维码

异常处理


(1) 通用异常信息

public enum CommonError {
    UNKOWN_ERROR("执行过程异常,请重试。"),
    PARAMS_ERROR("非法参数"),
    OBJECT_NULL("对象为空"),
    QUERY_NULL("查询结果为空"),
    REQUEST_NULL("请求参数为空");

    private String errMessage;

    public String getErrMessage() {
        return errMessage;
    }

    private CommonError( String errMessage) {
        this.errMessage = errMessage;
    }
}

(2) 自定义异常类

public class XueChengPlusException extends RuntimeException{
    private String errMessage;

    public XueChengPlusException() {
    }

    public XueChengPlusException(String message) {
        super(message);
        this.errMessage = message;
    }

    public static void cast(String message){
        throw new RuntimeException(message);
    }

    public static void cast(CommonError error){
        throw new RuntimeException(error.getErrMessage());
    }

}

(3) 错误响应参数包装类

public class RestErrorResponse {
    private String errMessage;

    public RestErrorResponse(String errMessage){
        this.errMessage= errMessage;
    }

    public String getErrMessage() {
        return errMessage;
    }

    public void setErrMessage(String errMessage) {
        this.errMessage = errMessage;
    }
}

(4) 全局异常处理器

@Slf4j
@ControllerAdvice	//控制器增强
//@RestControllerAdvice相当于@ControllerAdvice和@ResponseBody
public class GlobalExceptionHandler {

    /**
     * 对项目自定义异常类进行处理
     */
    @ResponseBody   //返回JSON
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) //响应状态码:500
    @ExceptionHandler(XueChengPlusException.class)  //指定异常类型
    public RestErrorResponse customException(XueChengPlusException e){

        //记录异常
        log.error("系统异常{}",e.getErrMessage(),e);

        //解析出异常信息
        String errMessage = e.getErrMessage();
        RestErrorResponse restErrorResponse = new RestErrorResponse(errMessage);
        return restErrorResponse;
    }


    /**
     * 对其他异常进行处理
     */
    @ResponseBody   //返回JSON
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) //响应状态码:500
    @ExceptionHandler(Exception.class)  //指定异常类型
    public RestErrorResponse exception(Exception e){

        //记录异常
        log.error("系统异常{}",e.getMessage(),e);

        //解析出异常信息
        RestErrorResponse restErrorResponse = new RestErrorResponse(CommonError.UNKOWN_ERROR.getErrMessage());
        return restErrorResponse;
    }

}

(5)修改异常处理--新需求

//修改异常处理类
//修改前---->{"errMessage":"课程计划信息还有子级信息,无法操作"}
//修改后---->{"errCode":"120409","errMessage":"课程计划信息还有子级信息,无法操作"}
/**
 * @Author: ciky
 * @Description: 本项目自定义异常类型
 * @DateTime: 2024/5/24 19:45
 **/
@Data
public class XueChengPlusException extends RuntimeException{
    private String errMessage;
    private String errCode;		//新增--------------------------------------

    public XueChengPlusException() {
    }

    public XueChengPlusException(String message) {
        super(message);
        this.errMessage = message;
    }

	//新增--------------------------------------
    public XueChengPlusException(String code,String message) {
        super(message);
        this.errMessage = message;
        this.errCode = code;
    }

    public static void cast(String message){
        throw new XueChengPlusException(message);
    }

    //新增--------------------------------------
    public static void cast(String code,String message){
        throw new XueChengPlusException(code,message);
    }


    public static void cast(CommonError error){
        throw new XueChengPlusException(error.getErrMessage());
    }

}
/**
 * @Author: ciky
 * @Description: 全局类型处理器
 * @DateTime: 2024/5/24 20:21
 **/
@Slf4j
@ControllerAdvice
//@RestControllerAdvice相当于@ControllerAdvice和@ResponseBody
public class GlobalExceptionHandler {

    /**
     * 对项目自定义异常类进行处理
     */
    @ResponseBody   //返回JSON
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) //响应状态码
    @ExceptionHandler(XueChengPlusException.class)  //指定异常类型
    public RestErrorResponse customException(XueChengPlusException e){

        //记录异常
        log.error("系统异常{}",e.getErrMessage(),e);

        //解析出异常信息
        String errCode = e.getErrCode();	//新增--------------------------------------
        String errMessage = e.getErrMessage();
        if(errCode == null){
            RestErrorResponse restErrorResponse = new RestErrorResponse(errMessage);
            return restErrorResponse;
        }else{
            RestErrorResponse restErrorResponse = new RestErrorResponse(errCode, errMessage);	//新增-----------------------------------
            return restErrorResponse;
        }
    }
}
/**
 * @Author: ciky
 * @Description: 和前端约定返回的异常信息模型
 * @DateTime: 2024/5/24 19:43
 **/
@Data
public class RestErrorResponse {
    private String errCode;	//新增--------------------------------------
    private String errMessage;

    public RestErrorResponse(String errMessage){
        this.errMessage= errMessage;
    }
    
	//新增--------------------------------------
    public RestErrorResponse(String code,String errMessage){
        this.errCode= code;
        this.errMessage= errMessage;
    }

}