geode-client 0.1.1-alpha.20

Rust client library for Geode graph database with full GQL support
Documentation
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
//! Error types for the Geode client.

use thiserror::Error;

/// Result type alias for Geode operations
pub type Result<T> = std::result::Result<T, Error>;

/// Error types that can occur when using the Geode client
#[derive(Error, Debug)]
pub enum Error {
    /// Connection error
    #[error("Connection error: {0}")]
    Connection(String),

    /// Query execution error
    #[error("Query error: {code} - {message}")]
    Query { code: String, message: String },

    /// Authentication error
    #[error("Authentication error: {0}")]
    Auth(String),

    /// I/O error
    #[error("I/O error: {0}")]
    Io(#[from] std::io::Error),

    /// JSON serialization/deserialization error
    #[error("JSON error: {0}")]
    Json(#[from] serde_json::Error),

    /// QUIC connection error
    #[error("QUIC error: {0}")]
    Quic(String),

    /// TLS error
    #[error("TLS error: {0}")]
    Tls(String),

    /// Invalid DSN format
    #[error("Invalid DSN: {0}")]
    InvalidDsn(String),

    /// Type conversion error
    #[error("Type error: {0}")]
    Type(String),

    /// Timeout error
    #[error("Operation timed out")]
    Timeout,

    /// Pool error
    #[error("Pool error: {0}")]
    Pool(String),

    /// Input validation error
    #[error("Validation error: {0}")]
    Validation(String),

    /// Generic error
    #[error("{0}")]
    Other(String),
}

impl Error {
    /// Create a connection error
    pub fn connection<S: Into<String>>(msg: S) -> Self {
        Error::Connection(msg.into())
    }

    /// Create a query error
    pub fn query<S: Into<String>>(msg: S) -> Self {
        Error::Query {
            code: "QUERY_ERROR".to_string(),
            message: msg.into(),
        }
    }

    /// Create a protocol error
    pub fn protocol<S: Into<String>>(msg: S) -> Self {
        Error::Connection(format!("Protocol error: {}", msg.into()))
    }

    /// Create a transaction error
    pub fn transaction<S: Into<String>>(msg: S) -> Self {
        Error::Connection(format!("Transaction error: {}", msg.into()))
    }

    /// Create a timeout error
    pub fn timeout() -> Self {
        Error::Timeout
    }

    /// Create an auth error
    pub fn auth<S: Into<String>>(msg: S) -> Self {
        Error::Auth(msg.into())
    }

    /// Create a QUIC error
    pub fn quic<S: Into<String>>(msg: S) -> Self {
        Error::Quic(msg.into())
    }

    /// Create a TLS error
    pub fn tls<S: Into<String>>(msg: S) -> Self {
        Error::Tls(msg.into())
    }

    /// Create a type error
    pub fn type_error<S: Into<String>>(msg: S) -> Self {
        Error::Type(msg.into())
    }

    /// Create a pool error
    pub fn pool<S: Into<String>>(msg: S) -> Self {
        Error::Pool(msg.into())
    }

    /// Create a validation error
    pub fn validation<S: Into<String>>(msg: S) -> Self {
        Error::Validation(msg.into())
    }

    /// Create an invalid DSN error
    pub fn invalid_dsn<S: Into<String>>(msg: S) -> Self {
        Error::InvalidDsn(msg.into())
    }

    /// Check if the error is retryable
    ///
    /// Retryable errors are transient failures that may succeed on retry:
    /// - Connection errors (network issues)
    /// - Timeout errors
    /// - QUIC errors (connection reset, etc.)
    /// - Query errors with serialization failure codes (40001, 40P01)
    pub fn is_retryable(&self) -> bool {
        match self {
            Error::Connection(_) => true,
            Error::Timeout => true,
            Error::Quic(_) => true,
            Error::Query { code, .. } => {
                // ISO/IEC 39075 retryable codes
                code == "40001" || code == "40P01" || code == "40502"
            }
            Error::Pool(_) => true,
            _ => false,
        }
    }

    /// Get the error code if this is a query error
    pub fn code(&self) -> Option<&str> {
        match self {
            Error::Query { code, .. } => Some(code),
            _ => None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io;

    #[test]
    fn test_error_display_connection() {
        let err = Error::Connection("connection refused".to_string());
        assert_eq!(err.to_string(), "Connection error: connection refused");
    }

    #[test]
    fn test_error_display_query() {
        let err = Error::Query {
            code: "42000".to_string(),
            message: "syntax error".to_string(),
        };
        assert_eq!(err.to_string(), "Query error: 42000 - syntax error");
    }

    #[test]
    fn test_error_display_auth() {
        let err = Error::Auth("invalid credentials".to_string());
        assert_eq!(err.to_string(), "Authentication error: invalid credentials");
    }

    #[test]
    fn test_error_display_io() {
        let io_err = io::Error::new(io::ErrorKind::NotFound, "file not found");
        let err = Error::Io(io_err);
        assert!(err.to_string().starts_with("I/O error:"));
    }

    #[test]
    fn test_error_display_json() {
        let json_err: serde_json::Error = serde_json::from_str::<i32>("invalid").unwrap_err();
        let err = Error::Json(json_err);
        assert!(err.to_string().starts_with("JSON error:"));
    }

    #[test]
    fn test_error_display_quic() {
        let err = Error::Quic("connection reset".to_string());
        assert_eq!(err.to_string(), "QUIC error: connection reset");
    }

    #[test]
    fn test_error_display_tls() {
        let err = Error::Tls("certificate expired".to_string());
        assert_eq!(err.to_string(), "TLS error: certificate expired");
    }

    #[test]
    fn test_error_display_invalid_dsn() {
        let err = Error::InvalidDsn("missing host".to_string());
        assert_eq!(err.to_string(), "Invalid DSN: missing host");
    }

    #[test]
    fn test_error_display_type() {
        let err = Error::Type("cannot convert int to string".to_string());
        assert_eq!(err.to_string(), "Type error: cannot convert int to string");
    }

    #[test]
    fn test_error_display_timeout() {
        let err = Error::Timeout;
        assert_eq!(err.to_string(), "Operation timed out");
    }

    #[test]
    fn test_error_display_pool() {
        let err = Error::Pool("pool exhausted".to_string());
        assert_eq!(err.to_string(), "Pool error: pool exhausted");
    }

    #[test]
    fn test_error_display_other() {
        let err = Error::Other("unknown error".to_string());
        assert_eq!(err.to_string(), "unknown error");
    }

    #[test]
    fn test_error_from_io() {
        let io_err = io::Error::new(io::ErrorKind::ConnectionRefused, "refused");
        let err: Error = io_err.into();
        assert!(matches!(err, Error::Io(_)));
    }

    #[test]
    fn test_error_from_json() {
        let json_err: serde_json::Error = serde_json::from_str::<i32>("not_a_number").unwrap_err();
        let err: Error = json_err.into();
        assert!(matches!(err, Error::Json(_)));
    }

    #[test]
    fn test_error_helper_connection() {
        let err = Error::connection("test connection error");
        assert!(matches!(err, Error::Connection(msg) if msg == "test connection error"));
    }

    #[test]
    fn test_error_helper_query() {
        let err = Error::query("test query error");
        assert!(matches!(err, Error::Query { code, message }
            if code == "QUERY_ERROR" && message == "test query error"));
    }

    #[test]
    fn test_error_helper_protocol() {
        let err = Error::protocol("invalid frame");
        assert!(matches!(err, Error::Connection(msg) if msg.contains("Protocol error")));
    }

    #[test]
    fn test_error_helper_transaction() {
        let err = Error::transaction("rollback failed");
        assert!(matches!(err, Error::Connection(msg) if msg.contains("Transaction error")));
    }

    #[test]
    fn test_error_helper_timeout() {
        let err = Error::timeout();
        assert!(matches!(err, Error::Timeout));
    }

    #[test]
    fn test_error_helper_auth() {
        let err = Error::auth("bad token");
        assert!(matches!(err, Error::Auth(msg) if msg == "bad token"));
    }

    #[test]
    fn test_error_helper_quic() {
        let err = Error::quic("stream closed");
        assert!(matches!(err, Error::Quic(msg) if msg == "stream closed"));
    }

    #[test]
    fn test_error_helper_tls() {
        let err = Error::tls("handshake failed");
        assert!(matches!(err, Error::Tls(msg) if msg == "handshake failed"));
    }

    #[test]
    fn test_error_helper_type_error() {
        let err = Error::type_error("invalid cast");
        assert!(matches!(err, Error::Type(msg) if msg == "invalid cast"));
    }

    #[test]
    fn test_error_helper_pool() {
        let err = Error::pool("no connections available");
        assert!(matches!(err, Error::Pool(msg) if msg == "no connections available"));
    }

    #[test]
    fn test_error_is_retryable_connection() {
        let err = Error::Connection("network error".to_string());
        assert!(err.is_retryable());
    }

    #[test]
    fn test_error_is_retryable_timeout() {
        let err = Error::Timeout;
        assert!(err.is_retryable());
    }

    #[test]
    fn test_error_is_retryable_quic() {
        let err = Error::Quic("reset".to_string());
        assert!(err.is_retryable());
    }

    #[test]
    fn test_error_is_retryable_pool() {
        let err = Error::Pool("exhausted".to_string());
        assert!(err.is_retryable());
    }

    #[test]
    fn test_error_is_retryable_serialization_failure() {
        let err = Error::Query {
            code: "40001".to_string(),
            message: "serialization failure".to_string(),
        };
        assert!(err.is_retryable());
    }

    #[test]
    fn test_error_is_retryable_deadlock() {
        let err = Error::Query {
            code: "40P01".to_string(),
            message: "deadlock detected".to_string(),
        };
        assert!(err.is_retryable());
    }

    #[test]
    fn test_error_is_retryable_transaction_deadlock() {
        let err = Error::Query {
            code: "40502".to_string(),
            message: "transaction deadlock".to_string(),
        };
        assert!(err.is_retryable());
    }

    #[test]
    fn test_error_not_retryable_syntax() {
        let err = Error::Query {
            code: "42000".to_string(),
            message: "syntax error".to_string(),
        };
        assert!(!err.is_retryable());
    }

    #[test]
    fn test_error_not_retryable_auth() {
        let err = Error::Auth("invalid".to_string());
        assert!(!err.is_retryable());
    }

    #[test]
    fn test_error_not_retryable_tls() {
        let err = Error::Tls("cert error".to_string());
        assert!(!err.is_retryable());
    }

    #[test]
    fn test_error_not_retryable_dsn() {
        let err = Error::InvalidDsn("bad format".to_string());
        assert!(!err.is_retryable());
    }

    #[test]
    fn test_error_not_retryable_type() {
        let err = Error::Type("cast failed".to_string());
        assert!(!err.is_retryable());
    }

    #[test]
    fn test_error_code_query() {
        let err = Error::Query {
            code: "42000".to_string(),
            message: "syntax error".to_string(),
        };
        assert_eq!(err.code(), Some("42000"));
    }

    #[test]
    fn test_error_code_non_query() {
        let err = Error::Connection("test".to_string());
        assert_eq!(err.code(), None);
    }

    #[test]
    fn test_result_type_alias() {
        fn returns_result() -> Result<i32> {
            Ok(42)
        }
        assert_eq!(returns_result().unwrap(), 42);
    }

    #[test]
    fn test_result_type_alias_error() {
        fn returns_error() -> Result<i32> {
            Err(Error::Other("test".to_string()))
        }
        assert!(returns_error().is_err());
    }

    #[test]
    fn test_error_string_conversion() {
        // Test that Into<String> works for &str
        let err = Error::connection("test");
        assert!(matches!(err, Error::Connection(_)));

        // Test that Into<String> works for String
        let err = Error::connection(String::from("test"));
        assert!(matches!(err, Error::Connection(_)));
    }

    #[test]
    fn test_error_debug() {
        let err = Error::Connection("test".to_string());
        let debug_str = format!("{:?}", err);
        assert!(debug_str.contains("Connection"));
        assert!(debug_str.contains("test"));
    }
}