Skip to main content

connectorx/
errors.rs

1use crate::data_order::DataOrder;
2use std::any::type_name;
3use std::fmt;
4use thiserror::Error;
5
6/// Result type with [`ConnectorXError`] as the error.
7pub type Result<T> = std::result::Result<T, ConnectorXError>;
8/// Result type with [`ConnectorXOutError`] as the error.
9pub type OutResult<T> = std::result::Result<T, ConnectorXOutError>;
10
11/// Errors returned by the public entry points such as [`get_arrow`](crate::get_arrow::get_arrow). Wraps the errors of each source, destination and transport.
12#[derive(Error, Debug)]
13pub enum ConnectorXOutError {
14    /// A file in the request does not exist.
15    #[error("File {0} not found.")]
16    FileNotFoundError(String),
17
18    /// The connection string names a source that is not supported or not enabled.
19    #[error("Source {0} not supported.")]
20    SourceNotSupport(String),
21
22    /// The source does not support the requested `protocol`.
23    #[error("Protocol {protocol:?} is not supported for {db}; supported protocols: {supported}.")]
24    ProtocolNotSupported {
25        /// The protocol that was asked for.
26        protocol: String,
27        /// The source, e.g. `Postgres`.
28        db: String,
29        /// The protocols the source accepts, comma-separated.
30        supported: String,
31    },
32
33    /// I/O error.
34    #[error(transparent)]
35    IOError(#[from] std::io::Error),
36
37    /// JSON (de)serialization error.
38    #[error(transparent)]
39    JsonError(#[from] serde_json::Error),
40
41    /// Error from the JVM bridge used by the federated rewriter.
42    #[cfg(feature = "federation")]
43    #[error(transparent)]
44    J4RSError(#[from] j4rs::errors::J4RsError),
45
46    /// Error from DataFusion while running a federated query.
47    #[cfg(feature = "fed_exec")]
48    #[error(transparent)]
49    DataFusionError(#[from] datafusion::error::DataFusionError),
50
51    /// The connection string is not a valid URL.
52    #[error(transparent)]
53    UrlParseError(#[from] url::ParseError),
54
55    /// Internal ConnectorX error.
56    #[error(transparent)]
57    ConnectorXInternalError(#[from] ConnectorXError),
58
59    /// Error from the Postgres source.
60    #[cfg(feature = "src_postgres")]
61    #[error(transparent)]
62    PostgresSourceError(#[from] crate::sources::postgres::PostgresSourceError),
63
64    /// Error from the `postgres` client.
65    #[cfg(feature = "src_postgres")]
66    #[error(transparent)]
67    PostgresError(#[from] postgres::Error),
68
69    /// Error from the MySQL source.
70    #[cfg(feature = "src_mysql")]
71    #[error(transparent)]
72    MySQLSourceError(#[from] crate::sources::mysql::MySQLSourceError),
73
74    /// Error from the `mysql` client.
75    #[cfg(feature = "src_mysql")]
76    #[error(transparent)]
77    MysqlError(#[from] r2d2_mysql::mysql::Error),
78
79    /// Error from the SQL Server source.
80    #[cfg(feature = "src_mssql_common")]
81    #[error(transparent)]
82    MsSQLSourceError(#[from] crate::sources::mssql::MsSQLSourceError),
83
84    /// Error from the `tiberius` SQL Server client.
85    #[cfg(feature = "src_mssql_tiberius")]
86    #[error(transparent)]
87    MsSQL(#[from] tiberius::error::Error),
88
89    /// Error from the SQLite source.
90    #[cfg(feature = "src_sqlite")]
91    #[error(transparent)]
92    SQLiteSourceError(#[from] crate::sources::sqlite::SQLiteSourceError),
93
94    /// Error from the `rusqlite` client.
95    #[cfg(feature = "src_sqlite")]
96    #[error(transparent)]
97    SQLiteError(#[from] rusqlite::Error),
98
99    /// Error from the Oracle source.
100    #[cfg(feature = "src_oracle")]
101    #[error(transparent)]
102    OracleSourceError(#[from] crate::sources::oracle::OracleSourceError),
103
104    /// Error from the `oracle` client.
105    #[cfg(feature = "src_oracle")]
106    #[error(transparent)]
107    OracleError(#[from] r2d2_oracle::oracle::Error),
108
109    /// Error from the BigQuery source.
110    #[cfg(feature = "src_bigquery")]
111    #[error(transparent)]
112    BigQuerySourceError(#[from] crate::sources::bigquery::BigQuerySourceError),
113
114    /// Error from the BigQuery client.
115    #[cfg(feature = "src_bigquery")]
116    #[error(transparent)]
117    BigQueryError(#[from] gcp_bigquery_client::error::BQError),
118
119    /// Error from the Trino source.
120    #[cfg(feature = "src_trino")]
121    #[error(transparent)]
122    TrinoSourceError(#[from] crate::sources::trino::TrinoSourceError),
123
124    /// Error from the Arrow destination.
125    #[cfg(feature = "dst_arrow")]
126    #[error(transparent)]
127    ArrowError(#[from] crate::destinations::arrow::ArrowDestinationError),
128
129    /// Error from the streaming Arrow destination.
130    #[cfg(feature = "dst_arrow")]
131    #[error(transparent)]
132    ArrowStreamError(#[from] crate::destinations::arrowstream::ArrowDestinationError),
133
134    /// Error from the Postgres to Arrow transport.
135    #[cfg(all(feature = "src_postgres", feature = "dst_arrow"))]
136    #[error(transparent)]
137    PostgresArrowTransportError(#[from] crate::transports::PostgresArrowTransportError),
138
139    /// Error from the Postgres to streaming Arrow transport.
140    #[cfg(all(feature = "src_postgres", feature = "dst_arrow"))]
141    #[error(transparent)]
142    PostgresArrowStreamTransportError(#[from] crate::transports::PostgresArrowStreamTransportError),
143
144    /// Error from the MySQL to Arrow transport.
145    #[cfg(all(feature = "src_mysql", feature = "dst_arrow"))]
146    #[error(transparent)]
147    MySQLArrowTransportError(#[from] crate::transports::MySQLArrowTransportError),
148
149    /// Error from the MySQL to streaming Arrow transport.
150    #[cfg(all(feature = "src_mysql", feature = "dst_arrow"))]
151    #[error(transparent)]
152    MySQLArrowStreamTransportError(#[from] crate::transports::MySQLArrowStreamTransportError),
153
154    /// Error from the SQLite to Arrow transport.
155    #[cfg(all(feature = "src_sqlite", feature = "dst_arrow"))]
156    #[error(transparent)]
157    SQLiteArrowTransportError(#[from] crate::transports::SQLiteArrowTransportError),
158
159    /// Error from the SQLite to streaming Arrow transport.
160    #[cfg(all(feature = "src_sqlite", feature = "dst_arrow"))]
161    #[error(transparent)]
162    SQLiteArrowStreamTransportError(#[from] crate::transports::SQLiteArrowStreamTransportError),
163
164    /// Error from the SQL Server to Arrow transport.
165    #[cfg(all(feature = "src_mssql_common", feature = "dst_arrow"))]
166    #[error(transparent)]
167    MsSQLArrowTransportError(#[from] crate::transports::MsSQLArrowTransportError),
168
169    /// Error from the SQL Server to streaming Arrow transport.
170    #[cfg(all(feature = "src_mssql_common", feature = "dst_arrow"))]
171    #[error(transparent)]
172    MsSQLArrowStreamTransportError(#[from] crate::transports::MsSQLArrowStreamTransportError),
173
174    /// Error from the Oracle to Arrow transport.
175    #[cfg(all(feature = "src_oracle", feature = "dst_arrow"))]
176    #[error(transparent)]
177    OracleArrowTransportError(#[from] crate::transports::OracleArrowTransportError),
178
179    /// Error from the Oracle to streaming Arrow transport.
180    #[cfg(all(feature = "src_oracle", feature = "dst_arrow"))]
181    #[error(transparent)]
182    OracleArrowStreamTransportError(#[from] crate::transports::OracleArrowStreamTransportError),
183
184    /// Error from the BigQuery to Arrow transport.
185    #[cfg(all(feature = "src_bigquery", feature = "dst_arrow"))]
186    #[error(transparent)]
187    BigqueryArrowTransportError(#[from] crate::transports::BigQueryArrowTransportError),
188
189    /// Error from the BigQuery to streaming Arrow transport.
190    #[cfg(all(feature = "src_bigquery", feature = "dst_arrow"))]
191    #[error(transparent)]
192    BigqueryArrowStreamTransportError(#[from] crate::transports::BigQueryArrowStreamTransportError),
193
194    /// Error from the Trino to Arrow transport.
195    #[cfg(all(feature = "src_trino", feature = "dst_arrow"))]
196    #[error(transparent)]
197    TrinoArrowTransportError(#[from] crate::transports::TrinoArrowTransportError),
198
199    /// Error from the Trino to streaming Arrow transport.
200    #[cfg(all(feature = "src_trino", feature = "dst_arrow"))]
201    #[error(transparent)]
202    TrinoArrowStreamTransportError(#[from] crate::transports::TrinoArrowStreamTransportError),
203
204    /// Error from the ClickHouse source.
205    #[cfg(feature = "src_clickhouse")]
206    #[error(transparent)]
207    ClickHouseSourceError(#[from] crate::sources::clickhouse::ClickHouseSourceError),
208
209    /// Error from the ClickHouse to Arrow transport.
210    #[cfg(all(feature = "src_clickhouse", feature = "dst_arrow"))]
211    #[error(transparent)]
212    ClickHouseArrowTransportError(#[from] crate::transports::ClickHouseArrowTransportError),
213
214    /// Error from the ClickHouse to streaming Arrow transport.
215    #[cfg(all(feature = "src_clickhouse", feature = "dst_arrow"))]
216    #[error(transparent)]
217    ClickHouseArrowStreamTransportError(
218        #[from] crate::transports::ClickHouseArrowStreamTransportError,
219    ),
220
221    /// Any other errors that are too trivial to be put here explicitly.
222    #[error(transparent)]
223    Other(#[from] anyhow::Error),
224}
225
226/// Errors that can be raised from this library.
227#[derive(Error, Debug)]
228pub enum ConnectorXError {
229    /// The required type does not same as the schema defined.
230    #[error("Data type unexpected: {0:?} expected, {1} found.")]
231    TypeCheckFailed(String, &'static str),
232
233    /// The destination does not support the data order the source asked for.
234    #[error("Data order not supported {0:?}.")]
235    UnsupportedDataOrder(DataOrder),
236
237    /// Source and destination have no data order in common.
238    #[error("Cannot resolve data order: got {0:?} from source, {1:?} from destination.")]
239    CannotResolveDataOrder(Vec<DataOrder>, Vec<DataOrder>),
240
241    /// A parser could not produce a value of the requested type.
242    #[error("Cannot produce a {0}, context: {1}.")]
243    CannotProduce(&'static str, ProduceContext),
244
245    /// The transport has no rule to convert the source type into the destination type.
246    #[error("No conversion rule from {0} to {1}.")]
247    NoConversionRule(String, String),
248
249    /// The query is not a single `SELECT` statement.
250    #[error("Only support single query with SELECT statement, got {0}.")]
251    SqlQueryNotSupported(String),
252
253    /// The row count of a query could not be determined in advance.
254    #[error("Cannot get total number of rows in advance.")]
255    CountError(),
256
257    /// The query could not be parsed.
258    #[error(transparent)]
259    SQLParserError(#[from] sqlparser::parser::ParserError),
260
261    /// I/O error.
262    #[error(transparent)]
263    StdIOError(#[from] std::io::Error),
264
265    /// An environment variable is missing or not valid Unicode.
266    #[error(transparent)]
267    StdVarError(#[from] std::env::VarError),
268
269    /// Any other error.
270    #[error(transparent)]
271    Other(#[from] anyhow::Error),
272}
273
274impl ConnectorXOutError {
275    /// The error for a `protocol` that source `db` (e.g. `SourceType::Postgres`)
276    /// does not accept; `supported` lists the protocols it does.
277    pub fn protocol_not_supported(protocol: &str, db: impl fmt::Debug, supported: &[&str]) -> Self {
278        ConnectorXOutError::ProtocolNotSupported {
279            protocol: protocol.to_string(),
280            db: format!("{db:?}"),
281            supported: supported.join(", "),
282        }
283    }
284}
285
286impl ConnectorXError {
287    /// Build a [`ConnectorXError::CannotProduce`] for type `T`.
288    pub fn cannot_produce<T>(context: Option<String>) -> Self {
289        ConnectorXError::CannotProduce(type_name::<T>(), context.into())
290    }
291}
292
293/// Optional context attached to [`ConnectorXError::CannotProduce`].
294#[derive(Debug)]
295pub enum ProduceContext {
296    /// No context was given.
297    NoContext,
298    /// Free-form description of where the value came from.
299    Context(String),
300}
301
302impl From<Option<String>> for ProduceContext {
303    fn from(val: Option<String>) -> Self {
304        match val {
305            Some(c) => ProduceContext::Context(c),
306            None => ProduceContext::NoContext,
307        }
308    }
309}
310
311impl fmt::Display for ProduceContext {
312    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
313        match self {
314            ProduceContext::NoContext => write!(f, "No Context"),
315            ProduceContext::Context(s) => write!(f, "{}", s),
316        }
317    }
318}
319
320#[cfg(test)]
321mod tests {
322    use super::{ConnectorXError, ProduceContext};
323
324    #[test]
325    fn formats_produce_contexts() {
326        assert_eq!(
327            ProduceContext::from(None).to_string(),
328            ProduceContext::NoContext.to_string()
329        );
330        assert_eq!(
331            ProduceContext::from(Some("column 1".to_string())).to_string(),
332            "column 1"
333        );
334        assert_eq!(ProduceContext::NoContext.to_string(), "No Context");
335    }
336
337    #[test]
338    fn creates_type_specific_production_errors() {
339        let error = ConnectorXError::cannot_produce::<u32>(Some("value".to_string()));
340        assert_eq!(error.to_string(), "Cannot produce a u32, context: value.");
341    }
342}