Skip to content

错误处理 ​

Fluss Rust 客户端对所有可能失败的操作使用统一的 Error 类型和 Result<T> 别名。

基本用法 ​

rust
use fluss::error::{Error, Result};

// All operations return Result<T>
let conn = FlussConnection::new(config).await?;
let admin = conn.get_admin()?;
let table = conn.get_table(&table_path).await?;

使用 ? 运算符传播错误,或对特定变体进行 match 以做细粒度处理。

匹配错误变体 ​

rust
use fluss::error::Error;

match result {
    Ok(val) => {
        // handle success
    }
    Err(Error::RpcError { message, .. }) => {
        eprintln!("RPC failure: {}", message);
    }
    Err(Error::UnsupportedOperation { message }) => {
        eprintln!("Unsupported: {}", message);
    }
    Err(Error::FlussAPIError { api_error }) => {
        eprintln!("Server error: {}", api_error);
    }
    Err(e) => {
        eprintln!("Unexpected error: {}", e);
    }
}

错误变体 ​

变体说明
UnexpectedError带消息和可选 source 的通用意外错误
IoUnexpectedErrorI/O 错误(网络、文件系统)
RemoteStorageUnexpectedError远程存储错误(OpenDAL 后端失败)
RpcErrorRPC 通信失败(连接被拒绝、超时)
RowConvertErrorRow 转换失败(类型不匹配、数据非法)
ArrowErrorArrow 数据处理错误(schema 不匹配、编码问题)
IllegalArgument传给 API 方法的非法参数
UnsupportedOperation该表类型不支持的操作
FlussAPIErrorFluss 集群返回的服务端 API 错误

服务端错误以带特定错误码的 FlussAPIError 表示。可以使用 api_error() 辅助方法以更符合人体工程学的方式进行匹配:

rust
use fluss::error::FlussError;

match result {
    Err(ref e) if e.api_error() == Some(FlussError::InvalidTableException) => {
        eprintln!("Invalid table: {}", e);
    }
    Err(ref e) if e.api_error() == Some(FlussError::PartitionNotExists) => {
        eprintln!("Partition does not exist: {}", e);
    }
    Err(ref e) if e.api_error() == Some(FlussError::LeaderNotAvailableException) => {
        eprintln!("Leader not available: {}", e);
    }
    Err(ref e) if e.api_error() == Some(FlussError::AuthenticateException) => {
        eprintln!("Authentication failed: {}", e);
    }
    _ => {}
}

重试逻辑 ​

有些错误是暂时性的——服务端可能临时不可用、正在选举中或负载过高。可用 is_retriable() 判断是重试某个操作,还是将该错误视为永久性错误。

Error::is_retriable() 可直接在任何 Error 值上调用。RpcError 始终可重试;FlussAPIError 委托给服务端错误码判断;其他所有变体都返回 false。

rust
use fluss::error::Error;

match writer.append(&row) {
    Ok(_) => {}
    Err(ref e) if e.is_retriable() => {
        // Transient failure — safe to retry
    }
    Err(e) => {
        // Permanent failure — log and abort
        eprintln!("Fatal error: {}", e);
    }
}

可重试的变体 ​

变体 / 错误错误码原因
Error::RpcError—网络层失败,始终可重试
FlussError::NetworkException1服务端断开连接
FlussError::CorruptMessage3CRC 或大小错误
FlussError::SchemaNotExist9Schema 可能不存在
FlussError::LogStorageException10暂时的 log 存储错误
FlussError::KvStorageException11暂时的 KV 存储错误
FlussError::NotLeaderOrFollower12Leader 选举进行中
FlussError::CorruptRecordException14损坏的记录
FlussError::UnknownTableOrBucketException21元数据尚不可用
FlussError::RequestTimeOut25请求超时
FlussError::StorageException26暂时的存储错误
FlussError::NotEnoughReplicasAfterAppendException28已写入服务端但 ISR 规模偏低
FlussError::NotEnoughReplicasException29写入时 ISR 规模偏低
FlussError::LeaderNotAvailableException44分区无可用 leader

其他所有 Error 变体(例如 RowConvertError、IllegalArgument、UnsupportedOperation)的 is_retriable() 始终返回 false。

常见错误场景 ​

连接被拒绝 ​

Fluss 集群未运行,或地址不正确。

rust
let result = FlussConnection::new(config).await;
match result {
    Err(Error::RpcError { message, .. }) => {
        eprintln!("Cannot connect to cluster: {}", message);
    }
    _ => {}
}

表未找到 ​

表不存在或已被删除。

rust
use fluss::error::{Error, FlussError};

// Admin operations return FlussError::TableNotExist (code 7)
let result = admin.drop_table(&table_path, false).await;
match result {
    Err(ref e) if e.api_error() == Some(FlussError::TableNotExist) => {
        eprintln!("Table not found: {}", e);
    }
    _ => {}
}

// conn.get_table() wraps the error differently, match on FlussAPIError directly
let result = conn.get_table(&table_path).await;
match result {
    Err(Error::FlussAPIError { ref api_error }) => {
        eprintln!("Server error (code {}): {}", api_error.code, api_error.message);
    }
    _ => {}
}

分区未找到 ​

分区表上的分区不存在。

rust
use fluss::error::FlussError;

let result = admin.drop_partition(&table_path, &spec, false).await;
match result {
    Err(ref e) if e.api_error() == Some(FlussError::PartitionNotExists) => {
        eprintln!("Partition does not exist: {}", e);
    }
    _ => {}
}

认证失败 ​

SASL 凭据不正确,或用户不存在。

rust
use fluss::error::{Error, FlussError};

let result = FlussConnection::new(config).await;
match result {
    Err(ref e) if e.api_error() == Some(FlussError::AuthenticateException) => {
        eprintln!("Authentication failed: {}", e);
    }
    _ => {}
}

Schema 不匹配 ​

Row 数据与预期的表 schema 不符。

rust
let result = writer.append(&row);
match result {
    Err(Error::RowConvertError { .. }) => {
        eprintln!("Row does not match table schema");
    }
    _ => {}
}

在应用代码中使用 Result<T> ​

fluss::error::Result<T> 类型别名让你可以在应用函数中方便地用 ? 运算符处理 Fluss 错误:

rust
use fluss::error::Result;

async fn my_pipeline() -> Result<()> {
    let conn = FlussConnection::new(config).await?;
    let admin = conn.get_admin()?;
    let table = conn.get_table(&table_path).await?;
    let writer = table.new_append()?.create_writer()?;
    writer.append(&row)?;
    writer.flush().await?;
    Ok(())
}

对于同时使用其他错误类型和 Fluss 错误的应用,可以通过标准的 From / Into trait 转换,或使用 anyhow 之类的 crate:

rust
use anyhow::Result;

#[tokio::main]
async fn main() -> Result<()> {
    let conn = FlussConnection::new(config).await?;
    // fluss::error::Error implements std::error::Error,
    // so it converts into anyhow::Error automatically
    Ok(())
}

Fluss 中文文档