BaseExceptionInterface 接口:

public interface BaseExceptionInterface {
    String getErrorCode();    // 获取异常码
    String getErrorMessage(); // 获取异常信息
}

BizException 业务异常类:

import lombok.Getter;  
import lombok.Setter;  
  
@Getter  
@Setter  
public class BizException extends RuntimeException {  
    private String errorCode; // 异常状态吗  
    private String errorMessage; // 异常信息  
  
    public BizException(BaseExceptionInterface baseExceptionInterface) {  
        this.errorCode = baseExceptionInterface.getErrorCode();  
        this.errorMessage = baseExceptionInterface.getErrorMessage();  
    }  
}

使用样例

在各个微服务模块中定义具体的异常枚举:

public enum OrderErrorEnum implements BaseExceptionInterface {
    ORDER_NOT_FOUND("ORDER001", "订单不存在"),
    ORDER_STATUS_INVALID("ORDER002", "订单状态异常");
    
    private String code;
    private String message;
    
    // 构造方法和接口实现方法省略...
}
 
// 在业务代码中使用
if (order == null) {
    throw new BizException(OrderErrorEnum.ORDER_NOT_FOUND);
}