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

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

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

主要流程如下:

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

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

代码如下
BussinessException

@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

@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

/**  * 全局异常处理器  */  
@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, "系统错误");  }  
}

相关文章:

  • 为WIN10微软输入法的全角切换Bug禁用Shift+Space组合键
  • Redis 存储数据需要注意哪些问题?热 key 问题、大 key 问题怎么处理?
  • 天若 OCR效率工具F4 截图秒识别,多语言混合文本准确率高离线识别保隐私
  • LangChain-5-agent
  • (C++)C语言和C++的关系和区别(C++教学)(C语言)
  • 59-Oracle 10046事件-知识准备
  • Spring Boot微服务架构实战:从单体到分布式的完整转型指南
  • 外卖之后再度进军酒旅,京东多线出击的逻辑是什么?
  • 鸿蒙NEXT-鸿蒙老版本迁移最新版本(API19,modelVersion5.1.1)
  • Spring Boot的自动装配和自动配置
  • MCU量产高效烧录:BootLoader与App合并技巧
  • 深入解析 C++ 中的红黑树:原理、实现与应用
  • 跨平台轻量级RTSP服务:重构内网超低延迟直播体验
  • “氢键本征型材料 + 柔性电容应变片”方案分析
  • 跟着AI学习C# Day26
  • ZooKeeper 3.9.2 集群安装指南
  • python web开发-Flask数据库集成
  • pikachu靶场通关笔记42 不安全的URL重定向
  • SpringBoot电脑商城项目--显示购物车列表
  • 【无标题】使用 Chocolatey 安装 WSL 管理工具 LxRunOffline
  • 沈阳营销型网站制作/工业设计公司
  • 常德网站建设/高端营销型网站建设
  • 百度有个学习的网站建设叫什么/推广小程序
  • 速效成交型网站/宁波seo
  • 网站后台图片并排怎么做/百度搜索关键词优化
  • 制作好的网站必须申请/运营推广公司