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
108
109
110
111
112
113
114
115
116
117
118
119
120
//! Error handling patterns
//!
//! Demonstrates how to handle errors gracefully in fraiseql-wire applications.
use fraiseql_wire::Result;
#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt::init();
println!("fraiseql-wire error handling example");
println!();
// Uncomment to run against real database
/*
use fraiseql_wire::FraiseClient;
use futures::StreamExt;
// Example 1: Handle connection errors
println!("Example 1: Connection error handling");
match FraiseClient::connect("postgres://invalid/database").await {
Ok(client) => println!("Connected successfully"),
Err(e) => {
eprintln!("Connection failed: {}", e);
eprintln!("Error category: {}", e.category());
if e.is_retriable() {
eprintln!("This error might succeed on retry");
} else {
eprintln!("This error is not retriable");
}
}
}
println!();
// Example 2: Handle stream errors
println!("Example 2: Stream error handling");
let mut client = FraiseClient::connect("postgres://localhost/mydb").await?;
let mut stream = client
.query("user")
.execute()
.await?;
let mut row_count = 0;
let mut error_count = 0;
while let Some(item) = stream.next().await {
match item {
Ok(json) => {
// Process the JSON value
println!("Row {}: {:?}", row_count, json);
row_count += 1;
}
Err(e) => {
error_count += 1;
eprintln!("Error processing row: {}", e);
eprintln!("Error category: {}", e.category());
// Decide whether to continue or abort
match e.category() {
"json_decode" => {
eprintln!("Skipping malformed JSON row");
continue;
}
"io" => {
eprintln!("Network error, aborting");
break;
}
_ => {
eprintln!("Unknown error, aborting");
break;
}
}
}
}
}
println!("Processed {} rows, {} errors", row_count, error_count);
client.close().await?;
*/
println!("Error handling patterns in fraiseql-wire");
println!();
println!("1. Connection Errors:");
println!(" - Check e.category() to understand error type");
println!(" - Use e.is_retriable() to decide on retry logic");
println!(" - Connection errors are typically non-retriable");
println!();
println!("2. Stream Errors:");
println!(" - Handle each item individually");
println!(" - json_decode errors: skip and continue");
println!(" - io errors: may be retriable");
println!(" - sql errors: typically abort");
println!();
println!("3. Error Categories:");
println!(" - 'connection': connection issues");
println!(" - 'authentication': auth failures");
println!(" - 'protocol': protocol violations");
println!(" - 'sql': SQL syntax or execution errors");
println!(" - 'json_decode': invalid JSON in results");
println!(" - 'io': network or I/O errors");
println!(" - 'config': configuration issues");
println!(" - 'invalid_schema': schema constraint violations");
println!();
println!("4. Retriable vs Non-Retriable:");
println!(" - Retriable: Io, ConnectionClosed");
println!(" - Non-retriable: all others");
println!();
println!("5. Using Error Information:");
println!(" - error.to_string() for user-facing messages");
println!(" - error.category() for programmatic decisions");
println!(" - error.is_retriable() for retry logic");
println!();
println!("See CONTRIBUTING.md for instructions on running with a real database.");
Ok(())
}