首页 > 其他分享 >【项目实战】如何在项目中自定义错误码

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

时间:2024-09-18 22:49:31浏览次数:3  
标签:code description 自定义 项目 错误码 errorCode message public String

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

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

标签:code,description,自定义,项目,错误码,errorCode,message,public,String
From: https://blog.csdn.net/2303_76696898/article/details/142345412

相关文章

  • 硬核项目合集!适合外包的 12 个开源后台管理系统,统统拿去做私活
    1.D2admin开源地址:https://github.com/d2-projects/d2-admin文档地址:https://d2.pub/zh/doc/d2-admin/效果预览:https://d2.pub/d2-admin/preview/#/index开源协议:MIT2.vue-element-admin开源地址:https://github.com/PanJiaChen/vue-element-admin文档地址:https://panj......
  • 大项目函数调用详解
    os.path.relpath是什么os.path.relpath是Python中os.path模块的一个函数,用于获取两个路径之间的相对路径。作用:os.path.relpath(path,start)会返回从start目录到path目录的相对路径。如果不指定start,则默认从当前工作目录计算。path:目标路径,表示你想获取相对路径......
  • MERN 应用程序无法从 MongoDB 中删除项目
    应用程序无法从MongoDB中删除项目问题描述错误信息在MERN应用程序中尝试从MongoDB中删除项目时遇到问题。具体表现为执行删除操作后,项目未能从数据库中成功移除。错误信息可能包括数据库连接问题、权限不足、语法错误等。通过查看控制台输出或服务器日志,可以获取更详细的错误......
  • ReVancedPacker 项目安装与使用教程
    ReVancedPacker项目安装与使用教程引言ReVancedPacker是一个开源项目,旨在帮助用户轻松地定制和修改Android应用程序。通过ReVancedPacker,用户可以对流行的应用程序(如YouTube、Twitter等)进行定制,添加或删除功能,甚至修改应用的外观和行为。本教程将详细介绍如何安装和使用ReV......
  • 自定义日志注解,保存信息到数据库
    定义日志注解importjava.lang.annotation.*;/***@authorwzw*@version1.0*@Date2023-2-1717:31:19*/@Target({ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic@interfaceLog{/***描述*@return*/......
  • 领导最讨厌的四种项目经理类型
    海尔CEO说:企业的核心领导岗位一定不要用不成熟的人。PMP备考资料免费领取##下面我就跟大家聊聊,不成熟的项目经理的具体特征。▎01气球型所谓气球型的人,就是没有自驱力,像气球一样需要不断充气。没有自驱力的人,总是需要依靠别人来激励,依靠领导去逼、去追,依靠制度去驱动,这样的项目经理......
  • 计算机系统教学—django高校科研项目得分管理系统
    标题:django高校科研项目得分管理系统设计并实施一个基于Django的高校科研项目得分管理系统,旨在高效地组织、评估和追踪科研项目的进展及成果。系统的核心功能模块:1.用户与权限管理•角色分配:支持管理员、评审专家、项目负责人、参与者等多个角色,每个角色具有定制化的操作......
  • 敏捷项目管理工具:团队高效协作的关键
    ​在当今快节奏的市场环境中,企业必须具备快速响应变化的能力。无论是产品研发、软件开发还是市场推广,灵活应对变化和高效管理项目已成为企业成功的关键。于是,敏捷开发这一理念越来越多地被各行业团队采纳,成为了他们高效协作、及时交付项目的重要手段。 什么是敏捷项目管理?敏捷......
  • 基于springboot的就业信息管理系统。Javaee项目,springboot项目。
    演示视频:基于springboot的就业信息管理系统。Javaee项目,springboot项目。项目介绍:采用M(model)V(view)C(controller)三层体系结构,通过Spring+SpringBoot+Mybatis+Maven来实现,前端采用了layui框架。MySQL数据库作为系统数据储存平台,采用JDBC技术进行数据库连接,实现了基......
  • 基于SSM的抗疫物资管理系统(数据库表结构文档)。Javaee项目。
    演示视频基于SSM的抗疫物资管理系统(数据库表结构文档)。Javaee项目。项目介绍:采用M(model)V(view)C(controller)三层体系结构,通过Spring+SpringMvc+Mybatis+Jsp+Bootstrap来实现。MySQL数据库作为系统数据储存平台,采用JDBC技术进行数据库连接,实现了基于B/S结构的Web系......