封装全局异常处理

打印 上一主题 下一主题

主题 862|帖子 862|积分 2586

目录

1 定义错误码类

​        可以定义各种错误码枚举,比如业务,系统相关的报错信息
  1. /**
  2. * 错误代码
  3. * 错误码
  4. *
  5. * @author leovany
  6. * @date 2023/09/23
  7. */
  8. public enum ErrorCode {
  9.     SUCCESS(0, "success", ""),
  10.     ERROR_PARAMS(40000, "请求参数错误", ""),
  11.     ERROR_NULL(40001, "请求数据为空", ""),
  12.     ERROR_LOGIN(40100, "未登录", ""),
  13.     ERROR_NO_AUTH(41001, "无权限", ""),
  14.     ERROR_SYSTEM(50000, "系统内部异常", "")
  15.     ;
  16.     /**
  17.      * 错误码ID
  18.      */
  19.     private final int code;
  20.     /**
  21.      * 错误码信息
  22.      */
  23.     private final String message;
  24.     /**
  25.      * 错误码描述(详情)
  26.      */
  27.     private final String description;
  28.     ErrorCode(int code, String message, String description) {
  29.         this.code = code;
  30.         this.message = message;
  31.         this.description = description;
  32.     }
  33.     public int getCode() {
  34.         return code;
  35.     }
  36.     public String getMessage() {
  37.         return message;
  38.     }
  39.     public String getDescription() {
  40.         return description;
  41.     }
  42. }
复制代码
2 定义业务异常类


  • 相对于 java 的异常类,支持更多字段
    扩展了 code和description两个字段
  • 自定义构造函数,更灵活 / 快捷的设置字段
  1. import com.leovany.usercenter.common.ErrorCode;
  2. /**
  3. * 业务异常
  4. * 自定义业务异常类
  5. *
  6. * @author leovany
  7. * @date 2023/09/23
  8. */
  9. public class BusinessException extends RuntimeException {
  10.     /**
  11.      * 错误码
  12.      */
  13.     private final int code;
  14.     /**
  15.      * 描述
  16.      */
  17.     private final String description;
  18.     /**
  19.      * 业务异常
  20.      *
  21.      * @param message     信息
  22.      * @param code        错误码
  23.      * @param description 描述
  24.      */
  25.     public BusinessException(String message, int code, String description) {
  26.         super(message);
  27.         this.code = code;
  28.         this.description = description;
  29.     }
  30.     /**
  31.      * 业务异常
  32.      *
  33.      * @param errorCode 错误代码
  34.      */
  35.     public BusinessException(ErrorCode errorCode) {
  36.         super(errorCode.getMessage());
  37.         this.code = errorCode.getCode();
  38.         this.description = errorCode.getDescription();
  39.     }
  40.     /**
  41.      * 业务异常
  42.      *
  43.      * @param errorCode   错误代码
  44.      * @param description 描述
  45.      */
  46.     public BusinessException(ErrorCode errorCode, String description) {
  47.         super(errorCode.getMessage());
  48.         this.code = errorCode.getCode();
  49.         this.description = description;
  50.     }
  51.     public int getCode() {
  52.         return code;
  53.     }
  54.     public String getDescription() {
  55.         return description;
  56.     }
  57. }
复制代码
3 全局异常处理器


  • 通过Spring AOP实现,在调用方法前后进行额外的处理
  • 作用

    • 捕获代码中所有的异常,让前端得到更详细的业务报错信息
    • 屏蔽掉项目框架本身的异常,不暴露服务器的内部状态
    • 集中处理,比如还可以做记录日志

  1. import com.leovany.usercenter.common.ResultVO;
  2. import com.leovany.usercenter.common.ErrorCode;
  3. import com.leovany.usercenter.common.ResultUtils;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.springframework.web.bind.annotation.ExceptionHandler;
  6. import org.springframework.web.bind.annotation.RestControllerAdvice;
  7. /**
  8. * 全局异常处理类
  9. */
  10. @RestControllerAdvice
  11. @Slf4j
  12. public class GlobalExceptionHandler {
  13.     /**
  14.      * 处理异常-BusinessException
  15.      * @param e
  16.      * @return
  17.      */
  18.     @ExceptionHandler(BusinessException.class)
  19.     public ResultVO<?> businessExceptionHandler(BusinessException e){
  20.         log.error("businessException:" + e.getMessage(),e);
  21.         return ResultUtils.error(e.getCode(),e.getMessage(),e.getDescription());
  22.     }
  23.     /**
  24.      * 处理异常-RuntimeException
  25.      * @param e
  26.      * @return
  27.      */
  28.     @ExceptionHandler(RuntimeException.class)
  29.     public ResultVO<?> runtimeExceptionHandler(RuntimeException e){
  30.         log.error("runtimeException:" + e);
  31.         return ResultUtils.error(ErrorCode.ERROR_SYSTEM,e.getMessage());
  32.     }
  33. }
复制代码
4 使用

​        throw new BusinessException可以在方法中,任意地方抛出,很方便

  • 示例代码
  1. @PostMapping("/login")
  2. public ResultVO<User> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
  3.     String userAccount = userLoginRequest.getUserAccount();
  4.     String userPassword = userLoginRequest.getUserPassword();
  5.     if (StringUtils.isAnyBlank(userAccount, userPassword)) {
  6.         throw new BusinessException(ErrorCode.ERROR_PARAMS);
  7.     }
  8.     User user = userService.doLogin(userAccount, userPassword, request);
  9.     return ResultUtils.success(user);
  10. }
复制代码

  • 代码对比

5 前端请求效果


总结

​        通过封装全局异常处理,对异常信息做了统一处理,让前端得到更详细的业务信息,同时保证系统的安全性(不会暴露系统内部信息),在代码上对参数校验等方面提供更加方便的形式。
本文由博客一文多发平台 OpenWrite 发布!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

兜兜零元

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表