| Crates.io | rocketmq-error |
| lib.rs | rocketmq-error |
| version | 0.7.0 |
| created_at | 2025-05-19 08:43:29.407861+00 |
| updated_at | 2025-12-07 14:55:36.926559+00 |
| description | Rocketmq rust error module |
| homepage | https://github.com/mxsm/rocketmq-rust |
| repository | https://github.com/mxsm/rocketmq-rust |
| max_upload_size | |
| id | 1679461 |
| size | 104,118 |
Unified error handling system for RocketMQ Rust implementation - providing semantic, performant, and extensible error types.
🎉 New in v0.7.0: Complete unified error system with 8 semantic categories, performance optimizations, and backward compatibility!
From trait implementations for common errors[dependencies]
rocketmq-error = "0.7.0"
use rocketmq_error::{RocketMQError, RocketMQResult};
fn send_message(addr: &str) -> RocketMQResult<()> {
// Automatic error conversion from std::io::Error
let _config = std::fs::read_to_string("config.toml")?;
// Create semantic errors with convenience constructors
if addr.is_empty() {
return Err(RocketMQError::network_connection_failed(
"broker_addr",
"invalid address"
));
}
Ok(())
}
┌─────────────────────────────────────────────────────────┐
│ rocketmq-error (Core) │
├─────────────────────────────────────────────────────────┤
│ ┌──────────────────────────────────────────────────┐ │
│ │ RocketMQError (Main Enum) │ │
│ ├──────────────────────────────────────────────────┤ │
│ │ - Network (connection, timeout, etc) │ │
│ │ - Serialization (encode/decode) │ │
│ │ - Protocol (RPC, command validation) │ │
│ │ - Broker (broker operations) │ │
│ │ - Client (client operations) │ │
│ │ - Storage (persistence errors) │ │
│ │ - Configuration (config parsing) │ │
│ │ - State (invalid state) │ │
│ │ - Controller (Raft consensus) │ │
│ │ - IO (std::io::Error) │ │
│ └──────────────────────────────────────────────────┘ │
│ │
│ Type Alias: RocketMQResult<T> = Result<T, RocketMQError>
└─────────────────────────────────────────────────────────┘
▲ ▲ ▲
│ │ │
┌──────┴───┐ ┌──────┴───┐ ┌──────┴───┐
│ Client │ │ Broker │ │ Store │
│ Crate │ │ Crate │ │ Crate │
└──────────┘ └──────────┘ └──────────┘
┌──────────────────────────────────────┐
│ rocketmq-error (v0.7.0+) │
│ (Unified Error System) │
│ │
│ pub enum RocketMQError { │
│ • Network(NetworkError) │
│ • Serialization(SerializationErr) │
│ • Protocol(ProtocolError) │
│ • BrokerOperationFailed { .. } │
│ • TopicNotExist { topic } │
│ • ClientNotStarted │
│ • StorageReadFailed { .. } │
│ • ConfigMissing { key } │
│ • ControllerNotLeader { .. } │
│ • IO(std::io::Error) │
│ • Timeout { operation } │
│ • IllegalArgument { message } │
│ • Internal(String) │
│ } │
│ │
│ pub type RocketMQResult<T> = │
│ Result<T, RocketMQError>; │
└─────────────────┬────────────────────┘
│
┌─────────────────────────────────────┼─────────────────────────────────┐
│ │ │
▼ ▼ ▼
┌───────────────────────┐ ┌──────────────────────────┐ ┌──────────────────────┐
│ rocketmq-client │ │ rocketmq-broker │ │ rocketmq-remoting │
│ Uses: RocketMQError │ │ Uses: RocketMQError │ │ Uses: RocketMQError │
│ • Network errors │ │ • Broker operation errs │ │ • Network errors │
│ • Client state errors │ │ • Storage errors │ │ • Protocol errors │
└───────────────────────┘ └──────────────────────────┘ └──────────────────────┘
The unified error system provides 8 semantic categories with rich context:
Connection, timeout, send/receive failures:
RocketMQError::network_connection_failed("127.0.0.1:9876", "connection refused")
RocketMQError::Network(NetworkError::RequestTimeout { addr, timeout_ms })
Encoding/decoding failures:
RocketMQError::Serialization(SerializationError::DecodeFailed {
format: "protobuf",
message: "unexpected EOF"
})
RocketMQ protocol validation:
RocketMQError::Protocol(ProtocolError::InvalidCommand { code: 999 })
Broker operations and state:
RocketMQError::broker_operation_failed("SEND_MESSAGE", 1, "topic not exist")
.with_broker_addr("127.0.0.1:10911")
Client lifecycle and state:
RocketMQError::ClientNotStarted
RocketMQError::ProducerNotAvailable
Disk I/O and data corruption:
RocketMQError::storage_read_failed("/var/data/commitlog", "permission denied")
Config parsing and validation:
RocketMQError::ConfigMissing { key: "broker_addr" }
Distributed consensus:
RocketMQError::ControllerNotLeader { leader_id: Some(2) }
The unified error system is designed with the following principles:
rocketmq-error crateFrom trait&'static str for Known Strings// ❌ Bad: Heap allocation
#[error("Connection failed: {0}")]
ConnectionFailed(String),
// ✅ Good: Static string
#[error("Connection failed to {addr}")]
ConnectionFailed { addr: String },
// ❌ Bad: Always boxes
#[error(transparent)]
Other(#[from] Box<dyn std::error::Error>),
// ✅ Good: Only box when needed
#[error("Unexpected error: {0}")]
Unexpected(String),
impl From<std::io::Error> for RocketMQError {
#[inline]
fn from(e: std::io::Error) -> Self {
RocketMQError::IO(e)
}
}
Compared to legacy error system:
&'static str// Old
use rocketmq_error::RocketmqError;
// New
use rocketmq_error::{RocketMQError, RocketMQResult};
// Old
Err(RocketmqError::RemotingConnectError(addr))
// New
Err(RocketMQError::network_connection_failed(addr, "connection refused"))
// Old
match err {
RocketmqError::RemotingConnectError(addr) => { /* ... */ }
}
// New
match err {
RocketMQError::Network(NetworkError::ConnectionFailed { addr, reason }) => {
eprintln!("Failed to connect to {}: {}", addr, reason);
}
}
Before:
Err(RocketmqError::RemoteError("connection failed".to_string()))
After:
Err(RocketMQError::network_connection_failed(addr, "connection failed"))
Before:
Err(RocketmqError::RemotingSendRequestError(format!(
"Failed to send to {}: {}",
addr, reason
)))
After:
Err(RocketMQError::Network(NetworkError::SendFailed {
addr: addr.to_string(),
reason: reason.to_string(),
}))
Before:
std::fs::read(path).map_err(|e| RocketmqError::Io(e))?
After:
std::fs::read(path)? // Automatic conversion via From trait!
RocketMQError instead of RocketmqErrorNetworkErrorSerializationErrorProtocolErrorBrokerOperationFailedmap_err calls (use ? operator)cargo clippy to find deprecated usageThe legacy error system is still available but deprecated:
#[allow(deprecated)]
use rocketmq_error::RocketmqError; // Old error type still works
#[allow(deprecated)]
fn legacy_function() -> LegacyRocketMQResult<()> {
// ...
}
However, new code should use the unified error system.
impl RocketMQError {
pub fn network_connection_failed(addr: impl Into<String>, reason: impl Into<String>) -> Self {
RocketMQError::Network(NetworkError::ConnectionFailed {
addr: addr.into(),
reason: reason.into(),
})
}
pub fn broker_not_found(name: impl Into<String>) -> Self {
RocketMQError::BrokerNotFound {
name: name.into()
}
}
}
From// Define conversions from specific errors
impl From<serde_json::Error> for RocketMQError {
fn from(e: serde_json::Error) -> Self {
RocketMQError::Serialization(SerializationError::Json(e.to_string()))
}
}
// Usage: ? operator works automatically
fn parse_config(json: &str) -> RocketMQResult<Config> {
let config: Config = serde_json::from_str(json)?; // Auto-converts
Ok(config)
}
use tracing::error;
match result {
Err(RocketMQError::Network(NetworkError::ConnectionFailed { addr, reason })) => {
error!(
error.type = "network",
error.category = "connection_failed",
broker.addr = %addr,
error.reason = %reason,
"Failed to connect to broker"
);
}
_ => {}
}
// Bad
Err(RocketmqError::RemoteError("error".to_string()))
// Good
Err(RocketMQError::network_connection_failed(addr, "error"))
// Bad
let data = std::fs::read(path)
.map_err(|e| RocketMQError::IO(e))?;
// Good
let data = std::fs::read(path)?;
examples/ directory for usage patterns#[test]
fn test_error_conversion() {
let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
let rmq_err: RocketMQError = io_err.into();
assert!(matches!(rmq_err, RocketMQError::IO(_)));
}
#[test]
fn test_error_display() {
let err = RocketMQError::network_connection_failed("127.0.0.1:9876", "timeout");
assert!(err.to_string().contains("Connection failed"));
}
# Run all tests
cargo test -p rocketmq-error
# Run with all features
cargo test -p rocketmq-error --all-features
# Check documentation
cargo doc --open -p rocketmq-error
Current test status: ✅ 13/13 tests passing (10 unit tests + 3 doc tests)
v0.7.0 (2025-01-02) - 🎉 New unified error system
v0.6.x - Legacy error system (now deprecated)
RocketMQError)Licensed under Apache License, Version 2.0 or MIT license at your option.
Contributions are welcome! Please read our Contributing Guide for details on our code of conduct and the process for submitting pull requests.