Skip to main content

pgwire_replication/
error.rs

1//! Error types for pgwire-replication.
2//!
3//! All errors in this crate are represented by [`PgWireError`], which covers:
4//! - Configuration errors (invalid limits or connection settings)
5//! - I/O errors (network, file system)
6//! - Protocol errors (malformed messages, unexpected responses)
7//! - Server errors (PostgreSQL error responses)
8//! - Authentication errors (wrong password, unsupported method)
9//! - TLS errors (handshake failure, certificate issues)
10//! - Task errors (worker panics, unexpected termination)
11
12use std::sync::Arc;
13use thiserror::Error;
14
15/// Error type for all pgwire-replication operations.
16#[derive(Debug, Error, Clone)]
17pub enum PgWireError {
18    /// Invalid client configuration.
19    #[error("configuration error: {0}")]
20    Configuration(String),
21
22    /// I/O error (network, file system).
23    ///
24    /// Wraps `std::io::Error` in an `Arc` to preserve `ErrorKind` while
25    /// keeping `PgWireError` `Clone`. Use `.kind()` via `Arc`'s `Deref`.
26    #[error("io error: {0}")]
27    Io(Arc<std::io::Error>),
28
29    /// Protocol error - malformed message or unexpected response.
30    #[error("protocol error: {0}")]
31    Protocol(String),
32
33    /// Server error - PostgreSQL returned an error response.
34    ///
35    /// The message typically includes SQLSTATE code.
36    #[error("server error: {0}")]
37    Server(String),
38
39    /// Authentication error - wrong credentials or unsupported method.
40    #[error("authentication error: {0}")]
41    Auth(String),
42
43    /// TLS error - handshake failure, certificate validation, etc.
44    #[error("tls error: {0}")]
45    Tls(String),
46
47    /// Task error - worker panicked or terminated unexpectedly.
48    #[error("task error: {0}")]
49    Task(String),
50
51    /// Internal error - bug in the library.
52    #[error("internal error: {0}")]
53    Internal(String),
54}
55
56impl PgWireError {
57    /// Returns `true` if this is an I/O error.
58    #[inline]
59    pub fn is_io(&self) -> bool {
60        matches!(self, PgWireError::Io(_))
61    }
62
63    /// Returns `true` if this is a server error.
64    #[inline]
65    pub fn is_server(&self) -> bool {
66        matches!(self, PgWireError::Server(_))
67    }
68
69    /// Returns `true` if this is an authentication error.
70    #[inline]
71    pub fn is_auth(&self) -> bool {
72        matches!(self, PgWireError::Auth(_))
73    }
74
75    /// Returns `true` if this is a TLS error.
76    #[inline]
77    pub fn is_tls(&self) -> bool {
78        matches!(self, PgWireError::Tls(_))
79    }
80
81    /// Returns `true` if this error is likely transient and retryable.
82    ///
83    /// Transient errors include I/O errors and task errors. Non-transient
84    /// errors (auth, server, protocol) typically require configuration changes.
85    pub fn is_transient(&self) -> bool {
86        matches!(self, PgWireError::Io(_) | PgWireError::Task(_))
87    }
88}
89
90impl From<std::io::Error> for PgWireError {
91    fn from(err: std::io::Error) -> Self {
92        PgWireError::Io(Arc::new(err))
93    }
94}
95
96/// Result type alias for pgwire-replication operations.
97pub type Result<T> = std::result::Result<T, PgWireError>;