当前位置: 首页 > 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, "系统错误");  }  
}
http://www.lqws.cn/news/467461.html

相关文章:

  • 智慧水利数字孪生解决方案:百川孪生智领千行,100+标杆案例赋能智慧水利全域升级
  • 【LeetCode#第198题】打家劫舍(一维dp)
  • Compose笔记(二十七)--网格布局
  • SwinTransformer 改进:小波+注意力模块(Wavelet-Guided Attention)
  • 【LeetCode 热题 100】15. 三数之和——排序 + 双指针解法
  • WebeServer实现:学到了哪些东西
  • 缓存与加速技术实践-Kafka消息队列
  • Axios 在 Vue3 项目中的使用:从安装到组件中的使用
  • 【软考高级系统架构论文】论 SOA 在企业集成架构设计中的应用
  • 【软考高级系统架构论文】论多源数据集成及应用
  • AR眼镜与3D建模社区建设
  • stm32串口(uart)2转发到串口(uart)3实现
  • JVM知识点
  • 启动hardhat 项目,下载依赖的npm问题
  • React 虚拟dom
  • GNU Octave 基础教程(8):GNU Octave 常用数学函数
  • git的命令
  • IEC61850 通信协议测试验证方法详解
  • 经典:在浏览器地址栏输入信息到最终看到网页的全过程,涉及网络协议以及前后端技术
  • 我开源了一套springboot3快速开发模板
  • 八大架构宪法 - 技术使用指导说明文档
  • GitHub OAuth 认证示例
  • 人人都是音乐家?腾讯开源音乐生成大模型SongGeneration
  • springboot垃圾分类网站
  • 【Linux仓库】进程概念与基本操作【进程·贰】
  • 3D可视化数字孪生智能服务平台-物联网智控节能控、管、维一体化技术架构
  • C++11 std::thread 多线程编程详解
  • DeepSeek本地部署及应用方法
  • nacos热更新引起tcp激增导致服务不可用
  • 基于Python、tkinter、sqlite3 和matplotlib的校园书店管理系统