首页 > 其他分享 >SSM整合——项目异常处理

SSM整合——项目异常处理

时间:2023-03-23 21:56:15浏览次数:32  
标签:code final SSM static 整合 Integer 异常 public

项目异常处理

项目异常分类

业务异常

  • 不规范的用户行为产生的异常

  

  • 规范的用户行为产生的异常

  

系统异常

  • 项目运行过程中可预计且无法避免的异常

  

其他异常

  • 编程人员未预期到的异常

  

项目异常处理方案

业务异常

  • 发送对应的消息,传递给用户,提醒规范操作

系统异常

  • 发送固定消息传递给用户,安抚用户
  • 发送特定消息给运维人员,提醒维护
  • 记录日志

其他异常

  • 发送固定消息传递给用户,安抚用户
  • 发送特定消息给编程人员,提醒维护(纳入预期范围)
  • 记录日志

实现步骤

自定义项目系统异常

package com.cqupt.exception;

public class SystemException extends RuntimeException{
    private Integer code;
    
    public SystemException(Integer code, String message) {
        super(message);
        this.code = code;
    }


    public SystemException(Integer code, String message, Throwable cause) {
        super(message, cause);
        this.code = code;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

}

自定义项目业务异常

package com.cqupt.exception;

public class BusinessException extends RuntimeException{
    private Integer code;
    
    public BusinessException(Integer code, String message) {
        super(message);
        this.code = code;
    }


    public BusinessException(Integer code, String message, Throwable cause) {
        super(message, cause);
        this.code = code;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

}

自定义异常编码(持续补充)

package com.cqupt.controller;

public class Code {
    public static final Integer SAVE_OK = 20011;
    public static final Integer DELETE_OK = 20021;
    public static final Integer UPDATE_OK = 20031;
    public static final Integer SELECT_OK = 20041;

    public static final Integer SAVE_ERR = 20010;
    public static final Integer DELETE_ERR = 20020;
    public static final Integer UPDATE_ERR = 20030;
    public static final Integer SELECT_ERR = 20040;


    public static final Integer SYSTEM_ERR = 50001;
    public static final Integer SYSTEM_UNKNOW_ERR = 59999;
    public static final Integer BUSINESS_ERR = 60001;

}

拦截并处理异常

package com.cqupt.controller;

import com.cqupt.exception.BusinessException;
import com.cqupt.exception.SystemException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class ProjectExceptionAdvice {

    @ExceptionHandler(SystemException.class)
    public Result doSystemException(SystemException ex){
        // 记录日志
        // 发送信息给运维
        // 发送信息给编程人员
        return new Result(ex.getCode(),null,ex.getMessage());
    }

    @ExceptionHandler(BusinessException.class)
    public Result doBusinessException(BusinessException ex){
        // 传递信息给用户
        return new Result(ex.getCode(),null,ex.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public Result doException(Exception ex){
        // 记录日志
        // 发送信息给运维
        // 发送信息给编程人员
        return new Result(Code.SYSTEM_UNKNOW_ERR,null,"系统繁忙,请稍后再试");
    }
}

 

标签:code,final,SSM,static,整合,Integer,异常,public
From: https://www.cnblogs.com/1873cy/p/17249424.html

相关文章