项目中后端如何处理异常?

为了统一管理异常,在项目中封装了自定义异常类(BusinessException),全局异常处理器(GlobalExceptionHandler), 以及一些状态码(ErrorCode), 便于前端统一处理异常.

主要流程如下:

  1. 当项目业务发生逻辑错误时,会抛出BusinessException, 其中包含自定义错误码和信息.
  2. GlobalExceptionHandler 会捕获这个 BusinessException,并返回一个统一的响应给前端.
  3. 如果是未预料到的系统内部异常(RountimeException),GlobalExceptionHandler 也会捕捉, 并统一返回给前端错误码(50000) 和错误信息(系统内部异常).

这样便于便于前端快速定位错误原因, 便于在后续不同场景下精细化管理异常.

代码如下

BussinessException

java 复制代码
@Getter  
public class BusinessException extends RuntimeException {  
  
    /**  
     * 错误码  
     */  
    private final int code;  
  
    public BusinessException(int code, String message) {  
        super(message);  
        this.code = code;  
    }  
  
    public BusinessException(ErrorCode errorCode) {  
        super(errorCode.getMessage());  
        this.code = errorCode.getCode();  
    }  
  
    public BusinessException(ErrorCode errorCode, String message) {  
        super(message);  
        this.code = errorCode.getCode();  
    }  
  
}

ErrorCode

java 复制代码
@Getter  
public enum ErrorCode {  
  
    SUCCESS(0, "ok"),  
    PARAMS_ERROR(40000, "请求参数错误"),  
    NOT_LOGIN_ERROR(40100, "未登录"),  
    NO_AUTH_ERROR(40101, "无权限"),  
    NOT_FOUND_ERROR(40400, "请求数据不存在"),  
    FORBIDDEN_ERROR(40300, "禁止访问"),  
    SYSTEM_ERROR(50000, "系统内部异常"),  
    OPERATION_ERROR(50001, "操作失败");  
  
    /**  
     * 状态码  
     */  
    private final int code;  
  
    /**  
     * 信息  
     */  
    private final String message;  
  
    ErrorCode(int code, String message) {  
        this.code = code;  
        this.message = message;  
    }  
  
}

GlobalExceptionHeader

java 复制代码
/**  
 * 全局异常处理器  
 */  
@RestControllerAdvice  
@Slf4j  
public class GlobalExceptionHandler {  
  
    @ExceptionHandler(NotLoginException.class)  
    public BaseResponse<?> notLoginException(NotLoginException e) {  
        log.error("NotLoginException", e);  
        return ResultUtils.error(ErrorCode.NOT_LOGIN_ERROR, e.getMessage());  
    }  
  
    @ExceptionHandler(NotPermissionException.class)  
    public BaseResponse<?> notPermissionExceptionHandler(NotPermissionException e) {  
        log.error("NotPermissionException", e);  
        return ResultUtils.error(ErrorCode.NO_AUTH_ERROR, e.getMessage());  
    }  
  
    @ExceptionHandler(BusinessException.class)  
    public BaseResponse<?> businessExceptionHandler(BusinessException e) {  
        log.error("BusinessException", e);  
        return ResultUtils.error(e.getCode(), e.getMessage());  
    }  
  
    @ExceptionHandler(RuntimeException.class)  
    public BaseResponse<?> businessExceptionHandler(RuntimeException e) {  
        log.error("RuntimeException", e);  
        return ResultUtils.error(ErrorCode.SYSTEM_ERROR, "系统错误");  
    }  
}
相关推荐
海狸老先生2 小时前
Apache Tomcat样例目录session操纵漏洞解读
java·网络安全·tomcat
Jinkxs4 小时前
基础14-Java集合框架:掌握List、Set和Map的使用
java·list
遗憾皆是温柔5 小时前
3.JVM,JRE和JDK的关系是什么
java·开发语言·jvm·面试
洛可可白5 小时前
Spring Boot 应用结合 Knife4j 进行 API 分组授权管理配置
java·spring boot·后端
亲爱的非洲野猪5 小时前
ZooKeeper 深度实践:从原理到 Spring Boot 全栈落地
spring boot·zookeeper·java-zookeeper
22:30Plane-Moon7 小时前
初识SpringBoot
java·spring boot·后端
黄昏晓x7 小时前
数据结构----排序
java·数据结构·排序算法
97zz7 小时前
项目配置文件正确但是启动失败,报配置文件内容错误或中间件地址与实际不符
java·中间件·springboot
CodeUp.9 小时前
基于SpringBoot的OA办公系统的设计与实现
spring boot·后端·mybatis
小醉你真好9 小时前
Spring Boot + ShardingSphere 分库分表实战
java·spring boot·后端·mysql