Skip to main content

elefant_client/
error.rs

1use crate::protocol;
2use crate::protocol::FieldDescription;
3use std::error::Error;
4use std::fmt::{Display, Formatter};
5
6#[derive(Debug)]
7pub enum ElefantClientError {
8    IoError(std::io::Error),
9    PostgresMessageParseError(protocol::PostgresMessageParseError),
10    UnexpectedBackendMessage(String),
11    PostgresError(String),
12    UnsupportedFieldType {
13        desired_rust_type: &'static str,
14        postgres_field: FieldDescription,
15    },
16    DataTypeParseError {
17        original_error: Box<dyn Error + Sync + Send>,
18        column_index: usize,
19    },
20    NoResultsReturned,
21    UnexpectedNullValue {
22        postgres_field: FieldDescription,
23    },
24    NotEnoughColumns {
25        desired: usize,
26        actual: usize,
27    },
28    UnsupportedAuthenticationMethod(String),
29    TlsError(String),
30    BatchQueryUnexpectedEnd,
31}
32
33impl From<std::io::Error> for ElefantClientError {
34    fn from(value: std::io::Error) -> Self {
35        ElefantClientError::IoError(value)
36    }
37}
38
39impl From<protocol::PostgresMessageParseError> for ElefantClientError {
40    fn from(value: protocol::PostgresMessageParseError) -> Self {
41        ElefantClientError::PostgresMessageParseError(value)
42    }
43}
44
45impl Display for ElefantClientError {
46    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
47        match self {
48            ElefantClientError::IoError(e) => {
49                write!(f, "IO error: {e}")
50            }
51            ElefantClientError::PostgresMessageParseError(e) => {
52                write!(f, "Postgres message parse error: {e}")
53            }
54            ElefantClientError::UnexpectedBackendMessage(e) => {
55                write!(f, "Unexpected backend message: {e}")
56            }
57            ElefantClientError::PostgresError(e) => {
58                write!(f, "Postgres error: {e}")
59            }
60            ElefantClientError::UnsupportedFieldType {
61                postgres_field,
62                desired_rust_type,
63            } => {
64                write!(f, "Unsupported field type: {postgres_field:?} for desired rust type: {desired_rust_type}")
65            }
66            ElefantClientError::DataTypeParseError {
67                column_index,
68                original_error,
69            } => {
70                write!(
71                    f,
72                    "Error while parsing response data: {original_error:?} as index {column_index}"
73                )
74            }
75            ElefantClientError::NoResultsReturned => {
76                write!(f, "No results returned from query.")
77            }
78            ElefantClientError::UnexpectedNullValue { postgres_field } => {
79                write!(
80                    f,
81                    "Unexpected null value when processing field: {postgres_field:?}."
82                )
83            }
84            ElefantClientError::NotEnoughColumns { desired, actual } => {
85                write!(
86                    f,
87                    "Not enough columns returned. Desired: {desired}, Actual: {actual}"
88                )
89            }
90            ElefantClientError::UnsupportedAuthenticationMethod(method) => {
91                write!(f, "Unsupported authentication method: {method}")
92            }
93            ElefantClientError::TlsError(msg) => {
94                write!(f, "TLS error: {msg}")
95            }
96            ElefantClientError::BatchQueryUnexpectedEnd => {
97                write!(
98                    f,
99                    "Batch query ended unexpectedly: expected more result sets"
100                )
101            }
102        }
103    }
104}
105
106impl Error for ElefantClientError {}