pgwire_replication/
error.rs

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