• Springboot项目全局异常处理


    1.ErrorCode.java

    1. package com.hng.config.exception.error;
    2. /**
    3. * @Author: 郝南过
    4. * @Description: TODO
    5. * @Date: 2023/11/14 10:56
    6. * @Version: 1.0
    7. */
    8. public interface ErrorCode {
    9. String getCode();
    10. String getMessage();
    11. }

    2.ErrorEnum.java

    1. package com.hng.config.exception.error;
    2. public enum ErrorEnum implements ErrorCode {
    3. /**
    4. * 成功
    5. */
    6. SUCCESS("SUCCESS", "成功"),
    7. //*********************系统异常*********************//
    8. /**
    9. * 请求失败(外域请求等)
    10. */
    11. REQUEST_FAIL("REQUEST_FAIL", "请求失败"),
    12. /**
    13. * 系统异常
    14. */
    15. SYSTEM_ERROR("SYSTEM_ERROR", "系统异常"),
    16. /**
    17. * 操作超时
    18. */
    19. OP_TIMEOUT("OP_TIMEOUT", "操作超时,请重试"),
    20. /**
    21. * 操作冲突(乐观锁、并发)
    22. */
    23. OP_CONFLICT("OP_CONFLICT", "操作冲突"),
    24. /**
    25. * 数据库执行错误
    26. */
    27. DB_ERROR("DB_ERROR", "数据库执行错误"),
    28. //*********************业务类异常*********************//
    29. /**
    30. * 参数错误
    31. */
    32. PARAMETER_ERROR("PARAMETER_ERROR", "参数错误"),
    33. /**
    34. * 没有权限
    35. */
    36. NO_PRIVILEGE("NO_PRIVILEGE", "没有权限"),
    37. /**
    38. * 数据异常(数据校验不通过等)
    39. */
    40. DATA_ERROR("DATA_ERROR", "数据异常"),
    41. /**
    42. * 数据不存在(数据校验等)
    43. */
    44. DATA_NOT_FOUND("DATA_NOT_FOUND", "数据不存在"),
    45. /**
    46. * 数据已存在(数据校验等)
    47. */
    48. DATA_EXIST("DATA_EXIST", "数据已存在");
    49. /**
    50. * 结果码
    51. */
    52. private String code;
    53. /**
    54. * 结果信息
    55. */
    56. private String message;
    57. ErrorEnum(String code, String message) {
    58. this.code = code;
    59. this.message = message;
    60. }
    61. @Override
    62. public String getCode() {
    63. return this.code;
    64. }
    65. @Override
    66. public String getMessage() {
    67. return this.message;
    68. }
    69. }

    3.BizException.java

    1. package com.hng.config.exception;
    2. import com.hng.config.exception.error.ErrorEnum;
    3. import lombok.Data;
    4. /**
    5. * @Author: 郝南过
    6. * @Description: TODO
    7. * @Date: 2023/11/14 15:43
    8. * @Version: 1.0
    9. */
    10. @Data
    11. public class BizException extends RuntimeException {
    12. private String errorCode;
    13. public BizException(String msg) {
    14. super(msg);
    15. }
    16. public BizException(String errorCode, String msg) {
    17. super(msg);
    18. this.errorCode = errorCode;
    19. }
    20. public BizException(String errorCode,String msg,Throwable throwable) {
    21. super(msg, throwable);
    22. this.errorCode = errorCode;
    23. }
    24. public BizException(ErrorEnum errorEnum) {
    25. super(errorEnum.getMessage());
    26. this.errorCode = errorEnum.getCode();
    27. }
    28. public String getErrorCode() {
    29. return errorCode;
    30. }
    31. }

    4.GlobalExceptionHandler.java

    1. package com.hng.config.exception.advice;
    2. import com.hng.config.exception.BizException;
    3. import com.hng.config.response.Result;
    4. import lombok.extern.slf4j.Slf4j;
    5. import org.springframework.web.bind.annotation.ExceptionHandler;
    6. import org.springframework.web.bind.annotation.RestControllerAdvice;
    7. /**
    8. * @Author: 郝南过
    9. * @Description: 统一处理异常类
    10. * @Date: 2023/11/14 15:49
    11. * @Version: 1.0
    12. */
    13. @Slf4j
    14. @RestControllerAdvice //声明GlobalExceptionHandler作为全局异常处理类
    15. public class GlobalExceptionHandler {
    16. @ExceptionHandler(Exception.class) //@ExceptionHandler注解指定某个方法处理对应的错误类型,根据异常类型选择最精确的处理方法进行处理
    17. public Result exceptionHandler(Exception e){
    18. // 记录异常信息到日志文件
    19. log.error("An exception occurred: ", e);
    20. // 判断是否为自定义的异常类型
    21. if(e instanceof BizException){
    22. BizException bizException = (BizException)e;
    23. return Result.Result(bizException.getErrorCode(),e.getMessage(),null);
    24. }
    25. return Result.Result("500",e.getMessage(),null);
    26. }
    27. }

  • 相关阅读:
    Linux常用命令——chpasswd命令
    docker命令介绍,镜像制作,容器启动,进入容器操作等
    CSS宽度问题
    选择题练习
    数据库优化方法及思路分析
    C++ 【类和对象: 析构函数,拷贝构造函数,运算符重载 --2】
    c语言 编程及答案
    pytest框架二次开发
    高效构建 vivo 企业级网络流量分析系统
    一文搞懂Qt-MQTT开发
  • 原文地址:https://blog.csdn.net/shaogaiyue9745602/article/details/134403210