SwiftWeb一键配置Web项目

时间:2024-11-21 09:49:24

Commit 121b395
Preview
Give feedback
poboll
poboll
committed
30 minutes ago
feat: Refactor error handling module with strategy pattern for customizable team-specific error standards

  • Introduced the interface to enable custom error enumeration implementations.
  • Refactored and to enhance extensibility.
  • Optimized methods to adapt to the new error handling structure.
  • Updated the README with examples and guidelines for defining and using custom error codes.
    main
    1 parent
    4f4f6d5
    commit
    121b395
    File tree
    Filter files…
    README.md
    README_CN.md
    pom.xml
    src/main/java/com/caiths/swiftweb
    common
    BaseResponse.java
    Error.java
    ErrorCode.java
    ResultUtils.java
    exception
    BusinessException.java
    GlobalExceptionHandler.java
    9 files changed
    +185
    -26
    lines changed
    Search within code

‎README.md
+38

Original file line number Diff line number Diff line change
@@ -69,6 +69,44 @@ spring:
4. Refresh the dependencies.

Custom Error Codes ????

  • By implementing the Error interface, you can define your own team’s error handling specifications!
    Example:
      public enum ErrorCode implements Error {   
          /**
           * 状态码
           */
          private final int code;
      
          /**
           * 错误信息
           */
          private final String message;
          
          /**
           * 成功
           */
          SUCCESS(0, "ok"),
          
          /**
           * 请求参数错误
           */
          PARAMS_ERROR(40000, "请求参数错误")@Override
          public int getCode() {
              return code;
          }
          
          @Override
          public String getMessage() {
              return message;
          }
      }
    

Why Choose SwiftWeb? ????

SwiftWeb simplifies the web development process compared to traditional methods by integrating essential features such as API documentation, error code management, and exception handling right out of the box.
‎README_CN.md
+37

Original file line number Diff line number Diff line change
@@ -69,6 +69,43 @@

  1. 刷新依赖

自定义错误码????

  • 通过实现Error接口即的可定义属于自己的团队的错误处理规范!!!
    示例:
 public enum ErrorCode implements Error {   
     /**
      * 状态码
      */
     private final int code;
 
     /**
      * 错误信息
      */
     private final String message;
     
     /**
      * 成功
      */
     SUCCESS(0, "ok"),
     
     /**
      * 请求参数错误
      */
     PARAMS_ERROR(40000, "请求参数错误")@Override
     public int getCode() {
         return code;
     }
     
     @Override
     public String getMessage() {
         return message;
     }
 }

我们的优势????

相比传统的创建Web项目,无需整合接口文档,拥有丰富的错误码,灵活的全局异常处理器。
‎pom.xml
+11

Original file line number Diff line number Diff line change
@@ -17,6 +17,11 @@
Build web applications quickly with SwiftWeb.
https://github.com/poboll/swift-web

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- 许可证部分,使用 MIT License -->
<licenses>
    <license>

@@ -63,6 +68,12 @@

org.projectlombok
lombok
true


org.springframework.boot
spring-boot-starter-test
test

‎src/main/java/com/caiths/swiftweb/common/BaseResponse.java
+6
-3
Original file line number Diff line number Diff line change
@@ -9,10 +9,14 @@
*

  • @param

  • @author poboll

  • @version: 1.0

  • @description: 基反应
    */
    @Data
    public class BaseResponse implements Serializable {

    private static final long serialVersionUID = 4887078045405290846L;
    private int code;

    private T data;
    @@ -29,7 +33,6 @@ public BaseResponse(int code, T data) {
    this(code, data, “”);
    }

    public BaseResponse(ErrorCode errorCode) {
    this(errorCode.getCode(), null, errorCode.getMessage());
    public BaseResponse(Error errorCode) { this(errorCode.getCode(), null, errorCode.getMessage());
    }
    }
    }
    ‎src/main/java/com/caiths/swiftweb/common/Error.java
    +27
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    package com.caiths.swiftweb.common;
    /**

  • 错误返回接口

  • @author poboll

  • @version: 1.0

  • @description: 错误返回接口, 自定义错误枚举实现该接口即可
    /
    public interface Error {
    /
    *

    • 错误代码
    • @return int
      /
      int getCode();
      /
      *
    • 错误描述
    • @return {@link String}
      /
      String getMessage();
      /
      *
    • 系统错误 ()
      */
      int SYSTEM_ERROR = 50000;
      }
      ‎src/main/java/com/caiths/swiftweb/common/ErrorCode.java
      +30
      -6
      Original file line number Diff line number Diff line change
      @@ -1,19 +1,43 @@
      package com.caiths.swiftweb.common;

/**

  • 错误码

  • 同一返回错误码

  • @author poboll
    */
    public enum ErrorCode {
    public enum ErrorCode implements Error {

    /**

    • 成功
      /
      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, “操作失败”);

    /**
    @@ -22,7 +46,7 @@ public enum ErrorCode {
    private final int code;

    /**

    • 信息
    • 错误信息
      */
      private final String message;

@@ -31,12 +55,12 @@ public enum ErrorCode {
this.message = message;
}

@Override
public int getCode() {
    return code;
}
@Override
public String getMessage() {
    return message;
}

}
}
‎src/main/java/com/caiths/swiftweb/common/ResultUtils.java
+15
-12
Original file line number Diff line number Diff line change
@@ -10,42 +10,45 @@ public class ResultUtils {
/**
* 成功
*
* @param data
* @param
* @return
* @param data 数据
* @return {@link BaseResponse}<{@link T}>
*/
public static BaseResponse success(T data) {
return new BaseResponse<>(0, data, “ok”);
}

/**
 * 错误
 * 失败
 *
 * @param errorCode
 * @return
 * @param errorCode 错误代码
 * @return {@link BaseResponse}
 */
public static BaseResponse error(ErrorCode errorCode) {
public static BaseResponse error(Error errorCode) {
    return new BaseResponse<>(errorCode);
}

/**
 * 错误
 * 失败
 *
 * @param code
 * @param message
 * @return
 * @param code    代码
 * @param message 消息
 * @return {@link BaseResponse}
 */
public static BaseResponse error(int code, String message) {
    return new BaseResponse(code, null, message);
}

/**
 * 错误
 * 失败
 *
 * @param errorCode
 * @return
 * @param errorCode 错误代码
 * @param message   消息
 * @return {@link BaseResponse}
 */
public static BaseResponse error(ErrorCode errorCode, String message) {
public static BaseResponse error(Error errorCode, String message) {
    return new BaseResponse(errorCode.getCode(), null, message);
}

}
‎src/main/java/com/caiths/swiftweb/exception/BusinessException.java
+8
-4
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.caiths.swiftweb.exception;

import com.caiths.swiftweb.common.ErrorCode;
import com.caiths.swiftweb.common.Error;

/**

  • 自定义异常类
    @@ -10,24 +10,28 @@
    */
    public class BusinessException extends RuntimeException {

    private static final long serialVersionUID = -2970393916478025106L;
    /**

    • 错误码
      */
      private final int code;

    public BusinessException(int code, String message) {
    super(message);
    this.code = code;
    }

    public BusinessException(ErrorCode errorCode) {
    public BusinessException(Error errorCode) {
    super(errorCode.getMessage());
    this.code = errorCode.getCode();
    }

    public BusinessException(ErrorCode errorCode, String message) {
    public BusinessException(Error errorCode, String message) {
    super(message);
    this.code = errorCode.getCode();
    }

    public int getCode() {
    return code;
    }
    }
    }
    ‎src/main/java/com/caiths/swiftweb/exception/GlobalExceptionHandler.java
    +13
    -1
    Original file line number Diff line number Diff line change
    @@ -16,15 +16,27 @@
    @Slf4j
    public class GlobalExceptionHandler {

    /**

    • 业务异常处理程序
    • @param e e
    • @return {@link BaseResponse}<{@link ?}>
      */
      @ExceptionHandler(BusinessException.class)
      public BaseResponse<?> businessExceptionHandler(BusinessException e) {
      log.error("businessException: " + e.getMessage(), e);
      return ResultUtils.error(e.getCode(), e.getMessage());
      }

    /**

    • 运行时异常处理程序
    • @param e e
    • @return {@link BaseResponse}<{@link ?}>
      */
      @ExceptionHandler(RuntimeException.class)
      public BaseResponse<?> runtimeExceptionHandler(RuntimeException e) {
      log.error(“runtimeException”, e);
      return ResultUtils.error(ErrorCode.SYSTEM_ERROR, e.getMessage());
      }
      }
      }