1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//! # Error Handling
//!
//! Demonstrates the SDK's error types and how to match on them. Shows
//! connection errors (connecting to a non-existent port), validation errors
//! (sending to an empty channel), and error code matching with [`ErrorCode`]
//! variants (`Validation`, `Transient`, `Timeout`, `Authentication`).
//!
//! ## Expected Output
//!
//! ```text
//! Connection failed (expected): <error message>
//! Error code: Transient
//! Is retryable: true
//! Suggestion: <suggestion text>
//! Validation error (expected):
//! Code: Validation
//! Message: <validation message>
//! Suggestion: <suggestion text>
//! Got validation error
//! ```
//!
//! ## Running
//!
//! Requires a running KubeMQ broker on port 50000 for validation error demos.
//! The connection error demo intentionally uses a wrong port. Override with
//! `KUBEMQ_ADDRESS`:
//!
//! ```bash
//! KUBEMQ_ADDRESS=my-host:50000 cargo run --example error_handling
//! ```
use kubemq::prelude::*;
use kubemq::{ErrorCode, EventBuilder, KubemqError};
#[tokio::main]
async fn main() -> kubemq::Result<()> {
// Attempt to connect to a non-existent server to demonstrate errors
let result = KubemqClient::builder()
.host("localhost")
.port(59999) // unlikely port
.check_connection(true)
.build()
.await;
match result {
Ok(client) => {
println!("Connected (unexpected)");
client.close().await?;
}
Err(ref e) => {
println!("Connection failed (expected): {}", e);
println!(" Error code: {:?}", e.code());
println!(" Is retryable: {}", e.is_retryable());
println!(" Suggestion: {}", e.suggestion());
}
}
// Demonstrate validation errors
let client_result = KubemqClient::builder()
.host("localhost")
.port(50000)
.build()
.await;
if let Ok(client) = client_result {
// Send to empty channel -- validation error
let event = EventBuilder::new()
.channel("") // invalid
.body(b"test".to_vec())
.build();
match client.send_event(event).await {
Ok(()) => println!("Sent (unexpected for empty channel)"),
Err(KubemqError::Validation {
code,
message,
suggestion,
..
}) => {
println!("Validation error (expected):");
println!(" Code: {:?}", code);
println!(" Message: {}", message);
println!(" Suggestion: {}", suggestion);
}
Err(e) => println!("Other error: {}", e),
}
// Demonstrate error matching by code
let event = EventBuilder::new()
.channel("")
.body(b"test".to_vec())
.build();
if let Err(e) = client.send_event(event).await {
match e.code() {
ErrorCode::Validation => println!("Got validation error"),
ErrorCode::Transient => println!("Got transient error"),
ErrorCode::Timeout => println!("Got timeout error"),
ErrorCode::Authentication => println!("Got auth error"),
_ => println!("Got other error: {:?}", e.code()),
}
}
client.close().await?;
}
Ok(())
}