错误处理
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 的通用意外错误 |
IoUnexpectedError | I/O 错误(网络、文件系统) |
RemoteStorageUnexpectedError | 远程存储错误(OpenDAL 后端失败) |
RpcError | RPC 通信失败(连接被拒绝、超时) |
RowConvertError | Row 转换失败(类型不匹配、数据非法) |
ArrowError | Arrow 数据处理错误(schema 不匹配、编码问题) |
IllegalArgument | 传给 API 方法的非法参数 |
UnsupportedOperation | 该表类型不支持的操作 |
FlussAPIError | Fluss 集群返回的服务端 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::NetworkException | 1 | 服务端断开连接 |
FlussError::CorruptMessage | 3 | CRC 或大小错误 |
FlussError::SchemaNotExist | 9 | Schema 可能不存在 |
FlussError::LogStorageException | 10 | 暂时的 log 存储错误 |
FlussError::KvStorageException | 11 | 暂时的 KV 存储错误 |
FlussError::NotLeaderOrFollower | 12 | Leader 选举进行中 |
FlussError::CorruptRecordException | 14 | 损坏的记录 |
FlussError::UnknownTableOrBucketException | 21 | 元数据尚不可用 |
FlussError::RequestTimeOut | 25 | 请求超时 |
FlussError::StorageException | 26 | 暂时的存储错误 |
FlussError::NotEnoughReplicasAfterAppendException | 28 | 已写入服务端但 ISR 规模偏低 |
FlussError::NotEnoughReplicasException | 29 | 写入时 ISR 规模偏低 |
FlussError::LeaderNotAvailableException | 44 | 分区无可用 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(())
}