wasi-pg-client 0.1.0

PostgreSQL client library for WASI Preview 2
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
//! Error types for the PostgreSQL client.
//!
//! This module defines the [`PgError`] enum which represents all possible errors
//! that can occur when using the PostgreSQL client, along with supporting types:
//!
//! - [`PgServerError`] — structured PostgreSQL server error with all ErrorResponse fields
//! - [`TransportError`] — network/transport layer errors
//! - [`sqlstate`] — SQLSTATE error code constants and helpers
//! - [`retry`] — retry helpers for transient errors

pub mod retry;
pub mod server;
pub mod sqlstate;

use std::fmt;

use crate::protocol::ProtocolError;
use crate::types::Error as TypeConversionError;

pub use crate::transport::TransportError;
pub use server::PgServerError;

// Re-export PoolError from pg-pool for structured pool errors
// Note: This is a separate crate, so we use a string-based approach
// to avoid a circular dependency. PoolError variants are embedded
// as structured data via the PoolErrorVariant enum below.

/// Structured pool error variants, mirroring `pg-pool::PoolError`.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum PoolErrorVariant {
    Exhausted,
    Closed,
    CreateFailed(String),
    ResetFailed(String),
}

impl std::fmt::Display for PoolErrorVariant {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            PoolErrorVariant::Exhausted => write!(f, "connection pool exhausted"),
            PoolErrorVariant::Closed => write!(f, "connection pool is closed"),
            PoolErrorVariant::CreateFailed(msg) => {
                write!(f, "failed to create pool connection: {msg}")
            }
            PoolErrorVariant::ResetFailed(msg) => write!(f, "connection reset failed: {msg}"),
        }
    }
}

impl std::error::Error for PoolErrorVariant {}

// ---------------------------------------------------------------------------
// PgError
// ---------------------------------------------------------------------------

/// The main error type for the PostgreSQL client.
///
/// This enum distinguishes between different error categories:
///
/// - **Server errors** — PostgreSQL returned an `ErrorResponse` message with
///   structured fields (SQLSTATE code, severity, detail, hint, etc.).
/// - **Transport errors** — Network-level failures (connection refused, timeout,
///   TLS handshake failure, etc.).
/// - **Protocol errors** — Wire protocol violations (unexpected message, bad encoding).
/// - **Authentication errors** — Failed authentication (wrong password, unsupported method).
/// - **Type conversion errors** — Failed conversion between Rust and PostgreSQL types.
/// - **Configuration errors** — Invalid connection string or parameters.
/// - **Connection state errors** — Connection closed, wrong state for operation.
/// - **Column/row errors** — Column not found, index out of bounds, unexpected NULL.
/// - **Timeout** — Operation timed out.
/// - **Pool errors** — Connection pool exhaustion or management errors.
#[derive(Debug)]
#[non_exhaustive]
pub enum PgError {
    /// Error returned by the PostgreSQL server (ErrorResponse).
    ///
    /// Contains all fields from the ErrorResponse message: severity, SQLSTATE
    /// code, message, detail, hint, position, constraint, etc.
    ///
    /// Boxed to reduce the overall size of the enum since `PgServerError`
    /// contains many `String` and `Option` fields.
    Server(Box<PgServerError>),

    /// Wire protocol violation.
    Protocol(ProtocolError),

    /// Network/transport error.
    Transport(TransportError),

    /// Authentication failure.
    Auth(String),

    /// Type conversion error.
    TypeConversion(TypeConversionError),

    /// Configuration error (invalid connection string, missing required field, etc.).
    Config(String),

    /// Connection is closed.
    ConnectionClosed,

    /// Unexpected NULL value in a column that was expected to be non-null.
    UnexpectedNull { column: String },

    /// Column not found in the result set.
    ColumnNotFound { name: String },

    /// Column index out of bounds.
    ColumnIndexOutOfBounds { index: usize, count: usize },

    /// Operation timed out.
    Timeout,

    /// Connection pool error.
    Pool(PoolErrorVariant),

    /// Invalid connection state for the requested operation.
    InvalidState(String),

    /// The operation is not supported.
    Unsupported(String),

    /// Operation was cancelled.
    Cancelled,

    /// I/O error.
    Io(std::io::Error),

    /// TLS error.
    #[cfg(feature = "tls")]
    Tls(rustls::Error),

    /// Any other error not covered by the above variants.
    Other(String),
}

impl std::fmt::Display for PgError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            PgError::Server(e) => write!(f, "server error: {}", e),
            PgError::Protocol(e) => write!(f, "protocol error: {}", e),
            PgError::Transport(e) => write!(f, "transport error: {}", e),
            PgError::Auth(msg) => write!(f, "authentication error: {}", msg),
            PgError::TypeConversion(e) => write!(f, "type conversion error: {}", e),
            PgError::Config(msg) => write!(f, "configuration error: {}", msg),
            PgError::ConnectionClosed => write!(f, "connection closed"),
            PgError::UnexpectedNull { column } => write!(f, "unexpected NULL in column {}", column),
            PgError::ColumnNotFound { name } => write!(f, "column not found: {}", name),
            PgError::ColumnIndexOutOfBounds { index, count } => {
                write!(
                    f,
                    "column index {} out of bounds (have {} columns)",
                    index, count
                )
            }
            PgError::Timeout => write!(f, "operation timed out"),
            PgError::Pool(e) => write!(f, "pool error: {e}"),
            PgError::InvalidState(msg) => write!(f, "invalid connection state: {}", msg),
            PgError::Unsupported(msg) => write!(f, "unsupported: {}", msg),
            PgError::Cancelled => write!(f, "cancelled"),
            PgError::Io(e) => write!(f, "I/O error: {}", e),
            #[cfg(feature = "tls")]
            PgError::Tls(e) => write!(f, "TLS error: {}", e),
            PgError::Other(msg) => write!(f, "{}", msg),
        }
    }
}

impl std::error::Error for PgError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            PgError::Server(e) => Some(e.as_ref()),
            PgError::Protocol(e) => Some(e),
            PgError::Transport(e) => Some(e),
            PgError::TypeConversion(e) => Some(e),
            PgError::Io(e) => Some(e),
            PgError::Pool(e) => Some(e),
            #[cfg(feature = "tls")]
            PgError::Tls(e) => Some(e),
            _ => None,
        }
    }
}

// ---------------------------------------------------------------------------
// From impls
// ---------------------------------------------------------------------------

impl From<PgServerError> for PgError {
    fn from(e: PgServerError) -> Self {
        PgError::Server(Box::new(e))
    }
}

impl From<ProtocolError> for PgError {
    fn from(e: ProtocolError) -> Self {
        PgError::Protocol(e)
    }
}

impl From<TransportError> for PgError {
    fn from(e: TransportError) -> Self {
        PgError::Transport(e)
    }
}

impl From<TypeConversionError> for PgError {
    fn from(e: TypeConversionError) -> Self {
        PgError::TypeConversion(e)
    }
}

impl From<std::io::Error> for PgError {
    fn from(e: std::io::Error) -> Self {
        PgError::Io(e)
    }
}

#[cfg(feature = "tls")]
impl From<rustls::Error> for PgError {
    fn from(e: rustls::Error) -> Self {
        PgError::Tls(e)
    }
}

/// Backward-compatible alias for [`PgError`].
pub type Error = PgError;

// ---------------------------------------------------------------------------
// PgError methods
// ---------------------------------------------------------------------------

impl PgError {
    /// Check if the error indicates the connection is broken and cannot be
    /// recovered without reconnecting.
    pub fn is_connection_broken(&self) -> bool {
        match self {
            PgError::ConnectionClosed => true,
            PgError::Transport(TransportError::ConnectionReset) => true,
            PgError::Transport(TransportError::UnexpectedEof) => true,
            PgError::Transport(TransportError::ConnectionRefused) => true,
            PgError::Server(ref e) => {
                e.is_connection_exception() || e.is_admin_shutdown() || e.is_crash_shutdown()
            }
            PgError::Io(ref e) => {
                matches!(
                    e.kind(),
                    std::io::ErrorKind::ConnectionReset
                        | std::io::ErrorKind::ConnectionAborted
                        | std::io::ErrorKind::BrokenPipe
                        | std::io::ErrorKind::UnexpectedEof
                )
            }
            _ => false,
        }
    }

    /// Check if this error is potentially retryable without reconnecting.
    pub fn is_retryable(&self) -> bool {
        match self {
            PgError::Server(e) => e.is_serialization_failure() || e.is_deadlock_detected(),
            PgError::Transport(TransportError::Timeout) => true,
            PgError::Timeout => true,
            _ => false,
        }
    }

    /// Returns the SQLSTATE error code if this is a server error.
    pub fn code(&self) -> Option<&str> {
        match self {
            PgError::Server(e) => Some(&e.code),
            _ => None,
        }
    }

    /// Add context to an error by wrapping it in a descriptive message.
    pub fn context(self, msg: impl Into<String>) -> Self {
        PgError::Other(format!("{}: {}", msg.into(), self))
    }
}

/// A specialized `Result` type for client operations.
pub type Result<T> = std::result::Result<T, PgError>;

// ---------------------------------------------------------------------------
// Conversion from auth::AuthError
// ---------------------------------------------------------------------------

impl From<crate::auth::AuthError> for PgError {
    fn from(e: crate::auth::AuthError) -> Self {
        match e {
            crate::auth::AuthError::PasswordRequired => PgError::Auth("password required".into()),
            crate::auth::AuthError::UnsupportedSaslMechanisms(mechs) => {
                PgError::Auth(format!("unsupported SASL mechanisms: {mechs:?}"))
            }
            crate::auth::AuthError::Scram(msg) => PgError::Auth(format!("SCRAM error: {msg}")),
            crate::auth::AuthError::InsecureAuthentication { method } => PgError::Auth(format!(
                "refusing {method} authentication over an unencrypted transport"
            )),
            crate::auth::AuthError::ServerError(msg) => PgError::Other(msg),
            crate::auth::AuthError::UnexpectedMessage => {
                PgError::Auth("unexpected message during authentication".into())
            }
            crate::auth::AuthError::Protocol(p) => PgError::Protocol(p),
            crate::auth::AuthError::Transport(t) => PgError::Transport(t),
            crate::auth::AuthError::Io(i) => PgError::Io(i),
            crate::auth::AuthError::Utf8(u) => PgError::Other(u.to_string()),
        }
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    #[test]
    fn test_pg_error_display() {
        let err = PgError::Server(Box::new(PgServerError::from_fields(vec![
            (b'S', "ERROR".to_string()),
            (b'C', "23505".to_string()),
            (b'M', "duplicate key".to_string()),
        ])));
        let display = err.to_string();
        assert!(display.contains("server error"));
        assert!(display.contains("duplicate key"));
        assert!(display.contains("23505"));
    }

    #[test]
    fn test_pg_error_is_connection_broken() {
        let err = PgError::Server(Box::new(PgServerError::from_fields(vec![
            (b'S', "FATAL".to_string()),
            (b'C', "08006".to_string()),
            (b'M', "connection failure".to_string()),
        ])));
        assert!(err.is_connection_broken());

        assert!(PgError::Transport(TransportError::ConnectionReset).is_connection_broken());
        assert!(PgError::Transport(TransportError::UnexpectedEof).is_connection_broken());
        assert!(PgError::Transport(TransportError::ConnectionRefused).is_connection_broken());
        assert!(!PgError::Transport(TransportError::Timeout).is_connection_broken());
        assert!(PgError::ConnectionClosed.is_connection_broken());

        let err = PgError::Server(Box::new(PgServerError::from_fields(vec![
            (b'S', "FATAL".to_string()),
            (b'C', "57P01".to_string()),
            (b'M', "admin shutdown".to_string()),
        ])));
        assert!(err.is_connection_broken());

        let err = PgError::Server(Box::new(PgServerError::from_fields(vec![
            (b'S', "ERROR".to_string()),
            (b'C', "23505".to_string()),
            (b'M', "duplicate key".to_string()),
        ])));
        assert!(!err.is_connection_broken());
    }

    #[test]
    fn test_pg_error_is_retryable() {
        let err = PgError::Server(Box::new(PgServerError::from_fields(vec![
            (b'S', "ERROR".to_string()),
            (b'C', "40001".to_string()),
            (b'M', "serialization failure".to_string()),
        ])));
        assert!(err.is_retryable());

        let err = PgError::Server(Box::new(PgServerError::from_fields(vec![
            (b'S', "ERROR".to_string()),
            (b'C', "40P01".to_string()),
            (b'M', "deadlock detected".to_string()),
        ])));
        assert!(err.is_retryable());

        assert!(PgError::Transport(TransportError::Timeout).is_retryable());
        assert!(PgError::Timeout.is_retryable());

        let err = PgError::Server(Box::new(PgServerError::from_fields(vec![
            (b'S', "ERROR".to_string()),
            (b'C', "23505".to_string()),
            (b'M', "duplicate key".to_string()),
        ])));
        assert!(!err.is_retryable());
        assert!(!PgError::ConnectionClosed.is_retryable());
    }

    #[test]
    fn test_pg_error_code() {
        let err = PgError::Server(Box::new(PgServerError::from_fields(vec![
            (b'S', "ERROR".to_string()),
            (b'C', "23505".to_string()),
            (b'M', "duplicate key".to_string()),
        ])));
        assert_eq!(err.code(), Some("23505"));
        assert_eq!(PgError::ConnectionClosed.code(), None);
    }

    #[test]
    fn test_pg_error_context() {
        let err = PgError::Server(Box::new(PgServerError::from_fields(vec![
            (b'S', "ERROR".to_string()),
            (b'C', "23505".to_string()),
            (b'M', "duplicate key".to_string()),
        ])));
        let with_ctx = err.context("inserting user");
        match with_ctx {
            PgError::Other(msg) => {
                assert!(msg.contains("inserting user"));
                assert!(msg.contains("duplicate key"));
            }
            _ => panic!("expected Other variant with context"),
        }
    }

    #[test]
    fn test_from_protocol_error() {
        let err = PgError::from(ProtocolError::UnexpectedEof);
        assert!(matches!(err, PgError::Protocol(_)));
    }

    #[test]
    fn test_from_transport_error() {
        let err = PgError::from(TransportError::ConnectionRefused);
        assert!(matches!(err, PgError::Transport(_)));
    }

    #[test]
    fn test_from_io_error() {
        let err = PgError::from(std::io::Error::new(
            std::io::ErrorKind::BrokenPipe,
            "broken",
        ));
        assert!(matches!(err, PgError::Io(_)));
    }

    #[test]
    fn test_io_error_is_connection_broken() {
        let err = PgError::Io(std::io::Error::new(
            std::io::ErrorKind::BrokenPipe,
            "broken",
        ));
        assert!(err.is_connection_broken());

        let err = PgError::Io(std::io::Error::new(
            std::io::ErrorKind::ConnectionReset,
            "reset",
        ));
        assert!(err.is_connection_broken());

        let err = PgError::Io(std::io::Error::new(std::io::ErrorKind::TimedOut, "timeout"));
        assert!(!err.is_connection_broken());
    }

    #[test]
    fn test_error_source_chain() {
        let err = PgError::Server(Box::new(PgServerError::from_fields(vec![
            (b'S', "ERROR".to_string()),
            (b'C', "23505".to_string()),
            (b'M', "duplicate key".to_string()),
        ])));
        assert!(err.source().is_some());

        let err = PgError::Transport(TransportError::ConnectionReset);
        assert!(err.source().is_some());

        let err = PgError::ConnectionClosed;
        assert!(err.source().is_none());
    }

    #[test]
    fn test_unexpected_null_display() {
        let err = PgError::UnexpectedNull {
            column: "id".to_string(),
        };
        assert_eq!(err.to_string(), "unexpected NULL in column id");
    }

    #[test]
    fn test_column_not_found_display() {
        let err = PgError::ColumnNotFound {
            name: "email".to_string(),
        };
        assert_eq!(err.to_string(), "column not found: email");
    }

    #[test]
    fn test_column_index_out_of_bounds_display() {
        let err = PgError::ColumnIndexOutOfBounds { index: 5, count: 3 };
        assert!(err.to_string().contains("5"));
        assert!(err.to_string().contains("3"));
    }
}