hdbconnect_impl/base/
hdb_error.rs1use crate::protocol::parts::{ExecutionResults, ServerError};
2use thiserror::Error;
4
5#[derive(Error, Debug)] #[non_exhaustive]
9pub enum HdbError {
10 #[error("Initialization without TLS failed")]
12 Initialization {
13 source: Box<dyn std::error::Error + Send + Sync>,
15 },
17
18 #[error("Authentication failed")]
20 Authentication {
21 #[from]
23 source: Box<HdbError>,
24 },
26
27 #[error("Error occured in deserialization")]
30 Deserialization {
31 #[from]
33 source: serde_db::de::DeserializationError,
34 },
36
37 #[error("Error occured in serialization")]
39 Serialization {
40 #[from]
42 source: serde_db::ser::SerializationError,
43 },
45
46 #[error("Some error occured while decoding CESU-8")]
48 Cesu8,
49
50 #[error("Some error occured while decoding CESU-8")]
52 Cesu8AsBytes {
53 bytes: Vec<u8>,
54 },
56
57 #[error("Erroneous Connection Parameters")]
59 ConnParams {
60 source: Box<dyn std::error::Error + Send + Sync + 'static>,
62 },
64
65 #[error("Database server responded with an error")]
68 DbError {
69 #[from]
71 source: ServerError,
72 },
74
75 #[error("Decompression failed")]
77 Decompression {
78 #[from]
80 source: lz4_flex::block::DecompressError,
81 },
83
84 #[error("TLS setup failed because the server name was not valid")]
86 TlsInvalidDnsName {
87 #[from]
89 source: rustls::pki_types::InvalidDnsNameError,
90 },
91
92 #[error("Connection setup failed due to failing TLS initialization")]
94 TlsInit {
95 source: Box<dyn std::error::Error + Send + Sync>,
97 },
98
99 #[error(
101 "TLS set up failed, after setting up the TCP connection; is the database prepared for TLS?"
102 )]
103 TlsProtocol {
104 #[from]
106 source: rustls::Error,
107 },
108
109 #[error("Error occured while evaluating a HdbResponse or an HdbReturnValue")]
111 Evaluation(&'static str),
112
113 #[error("Database server responded with at least one error: \n{0}")]
115 ExecutionResults(ExecutionResults),
116
117 #[error("Implementation error: {}", _0)]
119 Impl(std::borrow::Cow<'static, str>),
120
121 #[error("Error occured in thread synchronization")]
124 Poison,
125
126 #[error("An error occurred on the server that requires the session to be terminated")]
128 SessionClosingTransactionError,
129
130 #[error(transparent)]
132 Io {
133 #[from]
135 source: std::io::Error,
136 },
138
139 #[error("Error occured with a command that was repeated after a reconnect")]
141 ErrorAfterReconnect {
142 source: std::io::Error,
143 second: Box<HdbError>,
144 },
145
146 #[error("Wrong usage: {}", _0)]
148 Usage(std::borrow::Cow<'static, str>),
149
150 #[error("Connection is broken")]
152 ConnectionBroken { source: Option<Box<HdbError>> },
153}
154
155pub type HdbResult<T> = std::result::Result<T, HdbError>;
157
158impl HdbError {
159 #[must_use]
185 pub fn server_error(&self) -> Option<&ServerError> {
186 match self {
187 Self::DbError {
188 source: server_error,
189 } => Some(server_error),
190 _ => None,
191 }
192 }
193
194 #[must_use]
196 pub fn inner(&self) -> Option<&dyn std::error::Error> {
197 match self {
198 Self::Authentication { source } => Some(source),
199 Self::Deserialization { source } => Some(source),
200 Self::Serialization { source } => Some(source),
201 Self::ConnParams { source } | Self::TlsInit { source } => Some(&**source),
202 Self::DbError { source } => Some(source),
203 Self::Decompression { source } => Some(source),
204 Self::TlsInvalidDnsName { source } => Some(source),
205 Self::Io { source } => Some(source),
206 Self::TlsProtocol { source } => Some(source),
207 _ => None,
208 }
209 }
210
211 pub(crate) fn conversion_error_into_bytes(&self) -> Option<&[u8]> {
212 match self {
213 Self::Cesu8AsBytes { bytes } => Some(bytes),
214 _ => None,
215 }
216 }
217
218 pub(crate) fn conn_params(error: Box<dyn std::error::Error + Send + Sync + 'static>) -> Self {
219 Self::ConnParams { source: error }
220 }
221
222 #[must_use]
224 pub fn display_with_inner(&self) -> String {
225 if let Some(e) = self.inner() {
226 format!("{}, caused by {:?}", &self, e)
227 } else {
228 format!("{}", &self)
229 }
230 }
231}
232
233#[cfg(feature = "sync")]
234impl<G> From<std::sync::PoisonError<G>> for HdbError {
235 fn from(_error: std::sync::PoisonError<G>) -> Self {
236 Self::Poison
237 }
238}
239
240#[macro_export]
241macro_rules! usage_err {
242 ($($arg:tt)*) => {{
243 $crate::HdbError::Usage(std::borrow::Cow::from(format!($($arg)*)))
244 }};
245}
246#[macro_export]
247macro_rules! impl_err {
248 ($($arg:tt)*) => {{
249 $crate::HdbError::Impl(std::borrow::Cow::from(format!($($arg)*)))
250 }};
251}