当前位置: 首页 > article >正文

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

在项目设计阶段,前端需要根据后端返回的响应来对前端页面进行渲染(比如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)尝试在项目中报错抛出异常

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


http://www.kler.cn/news/319604.html

相关文章:

  • VisualStudio编译时出现无法启动mt.exe
  • C++之spring
  • Codeforces Round 973 (Div. 2) C. Password Cracking
  • 抓取股票数据
  • 28岁打算转行靠谱么,这个年龄转行,有什么适合的行业么?
  • opencv滤波算法总结
  • Linux挂载命令
  • Docker 的安装部署与基本使用
  • Apache CVE-2021-41773 漏洞攻略
  • What is the new in C#11?
  • 验收测试:从需求到交付的全程把控!
  • BERT训练环节(代码实现)
  • 通过docker启动ElasticSearch后为ElasticSearch设置用户和密码
  • 分享课程:VUE数据可视化教程
  • vant_UI的选择时间小组件封装
  • excel VBA进行间比法设计
  • 运行python程序
  • 初识前端监控
  • C++如何进阶? -- 整理一些学习资料
  • 基于stm32物联网身体健康检测系统
  • LeetCode 909. 蛇梯棋
  • nlohmann json:读写json文件
  • c++优先级队列自定义排序实现方式
  • SDK3(note上)
  • NLP 文本分类任务核心梳理
  • Selenium点击元素的方法
  • 【深入学习Redis丨第六篇】Redis哨兵模式与操作详解
  • 电脑自带dll修复在哪里,dll丢失的6种解决方法总结
  • 免费与付费代理IP工具的优缺点分析
  • 遗忘的数学(拉格朗日乘子法、牛顿法)