Skip to main content

controller_error_integration/
controller_error_integration.rs

1// Copyright 2023 The RocketMQ Rust Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! # ControllerError Integration Example
16//!
17//! This example demonstrates how to use ControllerError integrated with RocketMQError.
18//!
19//! ## Usage
20//!
21//! ```rust
22//! use rocketmq_error::RocketMQError;
23//! use rocketmq_error::RocketMQResult;
24//!
25//! // Function returning controller error
26//! fn check_leader() -> RocketMQResult<()> {
27//!     // Automatically converts ControllerError to RocketMQError
28//!     Err(RocketMQError::controller_not_leader(Some(1)))
29//! }
30//!
31//! // Function using convenient constructors
32//! fn handle_request() -> RocketMQResult<String> {
33//!     // Check if controller is leader
34//!     if !is_leader() {
35//!         return Err(RocketMQError::controller_not_leader(None));
36//!     }
37//!
38//!     // Validate request
39//!     if request_invalid() {
40//!         return Err(RocketMQError::controller_invalid_request(
41//!             "missing broker_name",
42//!         ));
43//!     }
44//!
45//!     // Check timeout
46//!     if operation_timeout() {
47//!         return Err(RocketMQError::controller_timeout(5000));
48//!     }
49//!
50//!     Ok("Success".to_string())
51//! }
52//!
53//! // Error propagation works seamlessly
54//! fn process() -> RocketMQResult<()> {
55//!     check_leader()?; // Propagates controller error
56//!     handle_request()?; // Propagates any error
57//!     Ok(())
58//! }
59//! ```
60//!
61//! ## Available Constructor Methods
62//!
63//! ```rust
64//! use rocketmq_error::RocketMQError;
65//!
66//! // Not leader error
67//! let err = RocketMQError::controller_not_leader(Some(3));
68//!
69//! // Raft consensus error
70//! let err = RocketMQError::controller_raft_error("proposal timeout");
71//!
72//! // Metadata not found
73//! let err = RocketMQError::controller_metadata_not_found("broker-a");
74//!
75//! // Invalid request
76//! let err = RocketMQError::controller_invalid_request("missing field: broker_name");
77//!
78//! // Timeout error
79//! let err = RocketMQError::controller_timeout(5000);
80//!
81//! // Shutdown error
82//! let err = RocketMQError::controller_shutdown();
83//! ```
84//!
85//! ## Pattern Matching
86//!
87//! ```rust
88//! use rocketmq_error::ControllerError;
89//! use rocketmq_error::RocketMQError;
90//!
91//! fn handle_error(err: RocketMQError) {
92//!     match err {
93//!         RocketMQError::Controller(ControllerError::NotLeader { leader_id }) => {
94//!             if let Some(id) = leader_id {
95//!                 println!("Redirect to leader: {}", id);
96//!             } else {
97//!                 println!("No leader elected yet");
98//!             }
99//!         }
100//!         RocketMQError::Controller(ControllerError::Timeout { timeout_ms }) => {
101//!             println!("Operation timed out after {}ms", timeout_ms);
102//!         }
103//!         RocketMQError::Controller(ControllerError::Shutdown) => {
104//!             println!("Controller is shutting down");
105//!         }
106//!         _ => {
107//!             println!("Other error: {}", err);
108//!         }
109//!     }
110//! }
111//! ```
112
113use rocketmq_error::ControllerError;
114use rocketmq_error::RocketMQError;
115use rocketmq_error::RocketMQResult;
116
117fn main() {
118    println!("=== ControllerError Integration Example ===\n");
119
120    // Example 1: Using constructor methods
121    println!("1. Creating controller errors using constructor methods:");
122    let err1 = RocketMQError::controller_not_leader(Some(1));
123    println!("   Not leader: {}", err1);
124
125    let err2 = RocketMQError::controller_timeout(5000);
126    println!("   Timeout: {}", err2);
127
128    let err3 = RocketMQError::controller_invalid_request("missing broker_name");
129    println!("   Invalid request: {}", err3);
130
131    // Example 2: Automatic conversion
132    println!("\n2. Automatic conversion from ControllerError:");
133    let controller_err = ControllerError::Raft("consensus failed".to_string());
134    let rocketmq_err: RocketMQError = controller_err.into();
135    println!("   Converted error: {}", rocketmq_err);
136
137    // Example 3: Error propagation
138    println!("\n3. Error propagation in functions:");
139    match simulated_operation() {
140        Ok(_) => println!("   Operation succeeded"),
141        Err(e) => println!("   Operation failed: {}", e),
142    }
143
144    // Example 4: Pattern matching
145    println!("\n4. Pattern matching on errors:");
146    let errors = vec![
147        RocketMQError::controller_not_leader(Some(2)),
148        RocketMQError::controller_timeout(3000),
149        RocketMQError::controller_shutdown(),
150    ];
151
152    for err in errors {
153        print_error_details(err);
154    }
155
156    println!("\n=== Example completed successfully ===");
157}
158
159// Simulated operation that returns controller error
160fn simulated_operation() -> RocketMQResult<()> {
161    // Simulate a not leader scenario
162    Err(RocketMQError::controller_not_leader(None))
163}
164
165// Helper function to print error details
166fn print_error_details(err: RocketMQError) {
167    match err {
168        RocketMQError::Controller(ControllerError::NotLeader { leader_id }) => {
169            println!("   - Not leader error, leader_id: {:?}", leader_id);
170        }
171        RocketMQError::Controller(ControllerError::Timeout { timeout_ms }) => {
172            println!("   - Timeout error after {}ms", timeout_ms);
173        }
174        RocketMQError::Controller(ControllerError::Shutdown) => {
175            println!("   - Controller shutdown error");
176        }
177        _ => {
178            println!("   - Other error: {}", err);
179        }
180    }
181}