全局异常处理

在开发中,为了保证业务的正确性我们会对业务参数进行校验,不满足要求的参数采用throw new XxxException 来抛出异常,阻止程序继续执行,比如:用户名不可为空,密码错误等。这类异常是需要展示给用户看的,还有一类是程序抛出来的一些未知的异常,如:SQL异常,空指针异常等等,这类异常不能直接抛给用户,而是应该统一捕获后,封装成统一的错误提示返回给用户,如“系统内部异常啦”。

JSONresult类

用于返回给前端的json信息

//返回JSON结果
@Data
//建造者模式
//@Builder
public class JSONResult {

    private boolean success = true;

    private String message = "成功";

    //错误码,用来描述错误类型 ,1000 表示么有错误
    private String code = "1000";

    //返回的数据
    private Object data;

    /** 创建当前实例 **/
    public static JSONResult success(){
        return new JSONResult();
    }
    /** 创建当前实例 **/
    public static JSONResult success(Object obj){
        JSONResult instance = new JSONResult();
        instance.setData(obj);
        return instance;
    }

    public static JSONResult success(Object obj,String code){
        JSONResult instance = new JSONResult();
        instance.setCode(code);
        instance.setData(obj);
        return instance;
    }
    /** 创建当前实例 **/

    public static JSONResult error(String message,String code){
        JSONResult instance = new JSONResult();
        instance.setMessage(message);
        instance.setSuccess(false);
        instance.setCode(code);
        return instance;
    }

    public static JSONResult error(){
        JSONResult jsonResult = new JSONResult();
        jsonResult.setSuccess(false);
        return jsonResult;
    }

    /** 创建当前实例 **/
    public static JSONResult error(String message){
        return error(message,null);
    }

}

枚举类

保存错误信息和错误码

public enum ErrorCode {
    // 错误码和错误信息需要根据要去自己修改
    ERROR_SYSTEM_ERROR("500","系统内部异常"),
    ERROR_USERNAME_ISNULL("1001","用户名为空"),
    ERROR_PASSWORD_ISNULL("1002","密码为空");

    private String message;
    private String code;

    ErrorCode(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public String getCode() {
        return code;
    }
    public String getMessage() {
        return message+"["+code+"]";
    }
}

定义异常类

继承  RuntimeException

@Data
public class GlobleException extends RuntimeException{

    private ErrorCode errorCode;
    public GlobleException(String message){
        super(message);
    }

    public GlobleException(ErrorCode errorCode){
        super(errorCode.getMessage());
        this.errorCode = errorCode;
    }
}

异常处理类

@RestControllerAdvice
public class GlobleExceptionHandler {

    //拦截异常 : 这个注解就可以拦截器 GlobleException 异常
    @ExceptionHandler(GlobleException.class)
    public JSONResult globleException(GlobleException e){
        e.printStackTrace();
        ErrorCode errorCode = e.getErrorCode();
        // 防止传入空值,出现空指针
        if(errorCode != null){
            return JSONResult.error(errorCode.getMessage(), errorCode.getCode());
        }
        return JSONResult.error(e.getMessage());
    }

    //拦截器其他异常
    @ExceptionHandler(Exception.class)
    public JSONResult exception(Exception e){
        e.printStackTrace();
        return JSONResult.error(ErrorCode.ERROR_SYSTEM_ERROR.getMessage(),
                ErrorCode.ERROR_SYSTEM_ERROR.getCode());
    }
}

执行流程

在service层,判断发生异常,就throw一个异常GobalException,根据异常判断里面传定义的枚举

if(true) thorw new GlobleException(ErrorCode.ERROR_USERNAME_ISNULLNULL);

 传到GobalException类,赋值给里面的 ErrorCode,然后拦截器就会处理,判断ErrorCode空值,如果为空,就输出e的信息,如果不为空,即返回ErrorCode的信息。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值