【项目实战】如何在项目中自定义错误码

在项目设计阶段,前端需要根据后端返回的响应来对前端页面进行渲染(比如404页面),通过自定义错误码,可以及时收集并且处理异常信息。

自定义错误码几个具体的应用

1.错误信息统一处理:全局异常处理器可以将不同种类的异常转化为统一的错误信息格式,提供一致的错误响应给客户端,增强了用户体验。

2.错误日志记录:可以在全局异常处理器中记录异常信息,包括异常类型、发生时间、请求参数等,有助于排查问题和分析系统健康状况。

3.异常信息隐藏: 通过全局异常处理器,可以隐藏敏感信息,以防止敏感信息泄露到客户端。

项目实战

 1)首先编写响应类,也就是返回前端的数据类,包括其属性和构造方法。

@Data
public class BaseResponse<T> implements Serializable {

    private int code;

    private T data;

    private String message;

    private String description;
    public BaseResponse(int code, T data, String message,String description) {
        this.code = code;
        this.data = data;
        this.message = message;
        this.description= description;
    }

    public BaseResponse(int code, T data,String message) {
        this(code,data,"","");
    }

    public BaseResponse(ErrorCode errorCode) {
        this(errorCode.getCode(),null,errorCode.getMessage());
    }
}

 2)用ResultUtils类构造BaseResponse对象

public class ResultUtils {

    public static <T> BaseResponse sucess(T data){
        return new BaseResponse(0,data,"ok");
    }

    public static BaseResponse error(ErrorCode errorCode) {
        return new BaseResponse<>(errorCode);
    }

    public static BaseResponse error(ErrorCode errorCode,String message,String description){
        return new BaseResponse<>(errorCode.getCode(),null,message,description);
    }

    public static BaseResponse error(int errorCode,String message,String description){
        return new BaseResponse<>(errorCode,null,message,description);
    }

    public static BaseResponse error(ErrorCode errorCode,String description){
        return new BaseResponse<>(errorCode.getCode(),null,errorCode.getDescription(),description);
    }

}

 3)编写枚举类ErrorCode,在抛出异常时,可以传入特定的错误码枚举值作为参数,并且将错误码枚举的 message 的 description 属性作为异常类的message,简化抛出异常的代码。

public enum ErrorCode {

    FORBIDER(40101,"禁止访问",""),
    SUCESS(0,"ok",""),
    PARAMS_ERROR(40000,"请求参数错误",""),
    NULL_ERROR(40001,"请求数据为空",""),
    NO_AUTH(40101,"没有权限",""),
    NOT_LOGIN(40100,"没有登录",""),
    SYSTEM_ERROR(50000,"系统内部异常","");



    private final int code;
    private final String message;
    private final String description;

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

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public String getDescription() {
        return description;
    }
}

4)定义一个全局异常处理类,并且使用 @RestControllerAdvice(或者 @ControllerAdvice) 注解该类,表示这是一个全局异常处理器类然后针对每种异常类定义一个处理异常的方法,并且使用 @ExceptionHandler(异常类ass)注解标注这些方法,可以在这些方法中进行日志记录等具体的异常处理操作,并返回一个响应对象,便于前端识别并给用户友好的错误提示。

@RestControllerAdvice
@Slf4j
public class GlobaExceptionHandler {
    @ExceptionHandler(BusinessException.class)
    public BaseResponse businessExceptionHandler(BusinessException e){
        log.error("BusinessException" + e.getMessage(),e);
        return ResultUtils.error(e.getCode(),e.getMessage(),e.getDescription());
    }

    @ExceptionHandler(RuntimeException.class)
    public BaseResponse runtimeExceptionHandler(RuntimeException e){
        log.error("runtimeException",e);
        return ResultUtils.error(ErrorCode.SYSTEM_ERROR,e.getMessage(),"");
    }
}

 5)用BusinessException包装的异常类

public class BusinessException extends RuntimeException{
    private final int code;
    private final String description;

    public BusinessException(String message, int code, String description) {
        super(message);
        this.code = code;
        this.description = description;
    }

    public BusinessException(ErrorCode errorCode) {
        super(errorCode.getMessage());
        this.code = errorCode.getCode();
        this.description = errorCode.getDescription();
    }

    public BusinessException(ErrorCode errorCode,String description) {
        super(errorCode.getMessage());
        this.code = errorCode.getCode();
        this.description = description;
    }

    public int getCode() {
        return code;
    }

    public String getDescription() {
        return description;
    }
}

 6)尝试在项目中报错抛出异常

这样项目最终就使用了自定义异常类返回到前端。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值