Skip to main content

postgresql/
error.rs

1//! Error types for the `postgresql` crate.
2
3use postgresql_protocol::{DbError, error::Error as ProtocolError};
4
5/// The error type returned by every fallible operation in this crate.
6#[derive(Debug, thiserror::Error)]
7#[non_exhaustive]
8pub enum Error {
9    /// A server-reported error (`ErrorResponse`).
10    ///
11    /// Boxed to keep every `Result<T, Error>` small: [`DbError`] is ~250 bytes.
12    #[error("{0}")]
13    Server(Box<DbError>),
14
15    /// A wire-protocol, decode, encode or authentication error.
16    #[error(transparent)]
17    Protocol(ProtocolError),
18
19    /// An I/O error on the connection.
20    #[error("I/O error: {0}")]
21    Io(#[from] std::io::Error),
22
23    /// A TLS error.
24    #[error("TLS error: {0}")]
25    Tls(#[from] rustls::Error),
26
27    /// The connection string is invalid or unsupported.
28    #[error("configuration error: {0}")]
29    Config(String),
30
31    /// The connection or pool has been closed.
32    #[error("connection closed")]
33    Closed,
34
35    /// The connection failed while a result was still streaming.
36    ///
37    /// The message describes the underlying cause (protocol error, I/O
38    /// failure, ...).
39    #[error("connection failed: {0}")]
40    ConnectionFailed(String),
41
42    /// Acquiring a pooled connection timed out.
43    #[error("connection pool timed out")]
44    PoolTimedOut,
45
46    /// The connection pool has been closed.
47    #[error("connection pool closed")]
48    PoolClosed,
49
50    /// A query expected exactly one row but found none.
51    #[error("row not found")]
52    RowNotFound,
53
54    /// A column name was not present in the result set.
55    #[error("column not found: {0}")]
56    ColumnNotFound(String),
57
58    /// A `NULL` was found where a non-nullable value was expected.
59    #[error("unexpected NULL in column {0}")]
60    UnexpectedNull(String),
61
62    /// A value could not be decoded.
63    #[error("decode error: {0}")]
64    Decode(String),
65
66    /// A value could not be encoded.
67    #[error("encode error: {0}")]
68    Encode(String),
69
70    /// A value was not a valid `time` date/time.
71    #[error("time error: {0}")]
72    Time(#[from] time::Error),
73
74    /// A value was not a valid UUID.
75    #[error("uuid error: {0}")]
76    Uuid(#[from] uuid::Error),
77
78    /// A value was not valid JSON.
79    #[error("json error: {0}")]
80    Json(#[from] serde_json::Error),
81
82    /// A value was not a valid IP network.
83    #[error("ipnetwork error: {0}")]
84    IpNetwork(#[from] ipnetwork::IpNetworkError),
85
86    /// A string was not valid UTF-8.
87    #[error("invalid UTF-8: {0}")]
88    Utf8(#[from] std::str::Utf8Error),
89}
90
91impl From<DbError> for Error {
92    fn from(err: DbError) -> Self {
93        Error::Server(Box::new(err))
94    }
95}
96
97impl From<ProtocolError> for Error {
98    fn from(err: ProtocolError) -> Self {
99        match err {
100            ProtocolError::Server(db) => Error::Server(db),
101            other => Error::Protocol(other),
102        }
103    }
104}